Contents
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#import <UIKit/UIKit.h>
@interface UIBarButtonItem (Items)
/** 图片 高亮图片 */
+ (UIBarButtonItem *)itemWithImage:(UIImage *)image heghImage:(UIImage *)heghImage target:(id)target action:(SEL)action;
/** 图片 选择图片 */
+ (UIBarButtonItem *)itemWithImage:(UIImage *)image selImage:(UIImage *)selImage target:(id)target action:(SEL)action;
/** 图片 高亮图片 标题*/
+ (UIBarButtonItem *)itemWithImage:(UIImage *)image heghImage:(UIImage *)heghImage target:(id)target action:(SEL)action title:(NSString *)title;
@end
#import "UIBarButtonItem+Items.h"
@implementation UIBarButtonItem (Items)
/** 图片 高亮图片 */
+ (UIBarButtonItem *)itemWithImage:(UIImage *)image selImage:(UIImage *)selImage target:(id)target action:(SEL)action
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:image forState:UIControlStateNormal];
[btn setImage:selImage forState:UIControlStateSelected];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[btn sizeToFit];
UIView *contantView = [[UIView alloc] init];
contantView.frame = btn.bounds;
[contantView addSubview:btn];
return [[UIBarButtonItem alloc] initWithCustomView:contantView];
}
/** 图片 选择图片 */
+ (UIBarButtonItem *)itemWithImage:(UIImage *)image heghImage:(UIImage *)heghImage target:(id)target action:(SEL)action
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:image forState:UIControlStateNormal];
[btn setImage:heghImage forState:UIControlStateSelected];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[btn sizeToFit];
UIView *contantView = [[UIView alloc] init];
contantView.frame = btn.bounds;
[contantView addSubview:btn];
return [[UIBarButtonItem alloc] initWithCustomView:contantView];
}
/** 图片 高亮图片 标题*/
+ (UIBarButtonItem *)itemWithImage:(UIImage *)image heghImage:(UIImage *)heghImage target:(id)target action:(SEL)action title:(NSString *)title
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:image forState:UIControlStateNormal];
[btn setImage:heghImage forState:UIControlStateHighlighted];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[btn sizeToFit];
btn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
UIView *contantView = [[UIView alloc] init];
contantView.frame = btn.bounds;
[contantView addSubview:btn];
return [[UIBarButtonItem alloc] initWithCustomView:contantView];
}
@end
Contents