11
11
12
12
@implementation UIBarButtonItem (FlatUI)
13
13
14
+ - (void ) configureFlatButtonWithColor : (UIColor *)color
15
+ highlightedColor : (UIColor *)highlightedColor
16
+ cornerRadius : (CGFloat) cornerRadius {
17
+
18
+ [UIBarButtonItem configureItemOrProxy: self forFlatButtonWithColor: color highlightedColor: color cornerRadius: cornerRadius];
19
+
20
+ }
21
+
14
22
+ (void ) configureFlatButtonsWithColor : (UIColor *) color
15
23
highlightedColor : (UIColor *)highlightedColor
16
24
cornerRadius : (CGFloat) cornerRadius {
17
25
18
- UIImage *backButtonPortraitImage = [UIImage backButtonImageWithColor: color
19
- barMetrics: UIBarMetricsDefault
20
- cornerRadius: cornerRadius];
21
- UIImage *highlightedBackButtonPortraitImage = [UIImage backButtonImageWithColor: highlightedColor
22
- barMetrics: UIBarMetricsDefault
23
- cornerRadius: cornerRadius];
24
- UIImage *backButtonLandscapeImage = [UIImage backButtonImageWithColor: color
25
- barMetrics: UIBarMetricsLandscapePhone
26
- cornerRadius: 2 ];
27
- UIImage *highlightedBackButtonLandscapeImage = [UIImage backButtonImageWithColor: highlightedColor
28
- barMetrics: UIBarMetricsLandscapePhone
29
- cornerRadius: 2 ];
30
-
31
- id appearance = [UIBarButtonItem appearanceWhenContainedIn: [UINavigationBar class ], [UINavigationController class ], nil ];
32
-
33
- [appearance setBackButtonBackgroundImage: backButtonPortraitImage
34
- forState: UIControlStateNormal
35
- barMetrics: UIBarMetricsDefault];
36
- [appearance setBackButtonBackgroundImage: backButtonLandscapeImage
37
- forState: UIControlStateNormal
38
- barMetrics: UIBarMetricsLandscapePhone];
39
- [appearance setBackButtonBackgroundImage: highlightedBackButtonPortraitImage
40
- forState: UIControlStateHighlighted
41
- barMetrics: UIBarMetricsDefault];
42
- [appearance setBackButtonBackgroundImage: highlightedBackButtonLandscapeImage
43
- forState: UIControlStateHighlighted
44
- barMetrics: UIBarMetricsLandscapePhone];
45
-
46
- [appearance setBackButtonTitlePositionAdjustment: UIOffsetMake (1 .0f , 1 .0f ) forBarMetrics: UIBarMetricsDefault];
47
- [appearance setBackButtonTitlePositionAdjustment: UIOffsetMake (1 .0f , 1 .0f ) forBarMetrics: UIBarMetricsLandscapePhone];
48
-
49
- UIImage *buttonImage = [UIImage imageWithColor: color cornerRadius: cornerRadius];
50
- [appearance setBackgroundImage: buttonImage forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
51
-
52
- id toolbarAppearance = [UIBarButtonItem appearanceWhenContainedIn: [UIToolbar class ], nil ];
53
- [toolbarAppearance setBackgroundImage: [UIImage buttonImageWithColor: color cornerRadius: cornerRadius shadowColor: color shadowInsets: UIEdgeInsetsZero] forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
26
+ [self configureFlatButtonsWithColor: color highlightedColor: highlightedColor cornerRadius: cornerRadius whenContainedIn: [UINavigationBar class ], [UINavigationController class ], nil ];
54
27
}
55
28
56
29
+ (void ) configureFlatButtonsWithColor : (UIColor *) color
57
30
highlightedColor : (UIColor *)highlightedColor
58
31
cornerRadius : (CGFloat) cornerRadius
59
- whenContainedIn : (Class <UIAppearanceContainer>)ContainerClass
60
- {
61
-
32
+ whenContainedIn : (Class <UIAppearanceContainer>)containerClass , ... {
33
+ va_list vl;
34
+ va_start (vl, containerClass);
35
+ id appearance = [UIBarButtonItem appearanceWhenContainedIn: containerClass, nil ];
36
+ va_end (vl);
37
+ [UIBarButtonItem configureItemOrProxy: appearance forFlatButtonWithColor: color highlightedColor: color cornerRadius: cornerRadius];
38
+ }
39
+
40
+ - (void ) removeTitleShadow {
41
+ NSMutableDictionary *titleTextAttributes = [[self titleTextAttributesForState: UIControlStateNormal] mutableCopy ];
42
+ if (!titleTextAttributes) {
43
+ titleTextAttributes = [NSMutableDictionary dictionary ];
44
+ }
45
+ [titleTextAttributes setValue: [NSValue valueWithUIOffset: UIOffsetMake (0 , 0 )] forKey: UITextAttributeTextShadowOffset];
46
+ [self setTitleTextAttributes: titleTextAttributes forState: UIControlStateNormal];
47
+ }
48
+
49
+ // helper method, basically a wrapper to allow creating a custom UIAppearance method that doesn't conform to the usual naming style
50
+ + (void ) configureItemOrProxy : (id )appearance
51
+ forFlatButtonWithColor : (UIColor *)color
52
+ highlightedColor : (UIColor *)highlightedColor
53
+ cornerRadius : (CGFloat) cornerRadius {
62
54
UIImage *backButtonPortraitImage = [UIImage backButtonImageWithColor: color
63
55
barMetrics: UIBarMetricsDefault
64
56
cornerRadius: cornerRadius];
@@ -72,8 +64,6 @@ + (void) configureFlatButtonsWithColor:(UIColor *) color
72
64
barMetrics: UIBarMetricsLandscapePhone
73
65
cornerRadius: 2 ];
74
66
75
- id appearance = [UIBarButtonItem appearanceWhenContainedIn: ContainerClass, nil ];
76
-
77
67
[appearance setBackButtonBackgroundImage: backButtonPortraitImage
78
68
forState: UIControlStateNormal
79
69
barMetrics: UIBarMetricsDefault];
@@ -93,18 +83,7 @@ + (void) configureFlatButtonsWithColor:(UIColor *) color
93
83
UIImage *buttonImage = [UIImage imageWithColor: color cornerRadius: cornerRadius];
94
84
[appearance setBackgroundImage: buttonImage forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
95
85
96
- id toolbarAppearance = [UIBarButtonItem appearanceWhenContainedIn: ContainerClass, nil ];
97
- [toolbarAppearance setBackgroundImage: [UIImage buttonImageWithColor: color cornerRadius: cornerRadius shadowColor: color shadowInsets: UIEdgeInsetsZero] forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
98
-
99
86
}
100
87
101
- - (void ) removeTitleShadow {
102
- NSMutableDictionary *titleTextAttributes = [[self titleTextAttributesForState: UIControlStateNormal] mutableCopy ];
103
- if (!titleTextAttributes) {
104
- titleTextAttributes = [NSMutableDictionary dictionary ];
105
- }
106
- [titleTextAttributes setValue: [NSValue valueWithUIOffset: UIOffsetMake (0 , 0 )] forKey: UITextAttributeTextShadowOffset];
107
- [self setTitleTextAttributes: titleTextAttributes forState: UIControlStateNormal];
108
- }
109
88
110
89
@end
0 commit comments