-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIBarButtonItem+FlatUI.m
executable file
·110 lines (94 loc) · 5.81 KB
/
UIBarButtonItem+FlatUI.m
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// UIBarButtonItem+FlatUI.m
// FlatUI
//
// Created by Jack Flintermann on 5/8/13.
// Copyright (c) 2013 Jack Flintermann. All rights reserved.
//
#import "UIBarButtonItem+FlatUI.h"
#import "UIImage+FlatUI.h"
@implementation UIBarButtonItem (FlatUI)
- (void) configureFlatButtonWithColor:(UIColor *)color
highlightedColor:(UIColor *)highlightedColor
cornerRadius:(CGFloat) cornerRadius {
[UIBarButtonItem configureItemOrProxy:self forFlatButtonWithColor:color highlightedColor:highlightedColor cornerRadius:cornerRadius];
}
+ (void) configureFlatButtonsWithColor:(UIColor *) color
highlightedColor:(UIColor *)highlightedColor
cornerRadius:(CGFloat) cornerRadius {
[self configureFlatButtonsWithColor:color highlightedColor:highlightedColor cornerRadius:cornerRadius whenContainedIn:[UINavigationBar class], [UINavigationController class], [UIToolbar class], nil];
}
+ (void) configureFlatButtonsWithColor:(UIColor *) color
highlightedColor:(UIColor *)highlightedColor
cornerRadius:(CGFloat) cornerRadius
whenContainedIn:(Class <UIAppearanceContainer>)containerClass, ... {
va_list vl;
va_start(vl, containerClass);
id appearance = [UIBarButtonItem appearanceWhenContainedIn:containerClass, nil];
va_end(vl);
[UIBarButtonItem configureItemOrProxy:appearance forFlatButtonWithColor:color highlightedColor:highlightedColor cornerRadius:cornerRadius];
}
- (void) removeTitleShadow {
NSArray *states = @[@(UIControlStateNormal), @(UIControlStateHighlighted)];
for (NSNumber *state in states) {
UIControlState controlState = [state unsignedIntegerValue];
NSMutableDictionary *titleTextAttributes = [[self titleTextAttributesForState:controlState] mutableCopy];
if (!titleTextAttributes) {
titleTextAttributes = [NSMutableDictionary dictionary];
}
/*** UNEXPECTED ***
UITextAttributeShadowOffset is deprecated in 5.0 - replaced with NSShadow
- tried using NSShadow, but the shadow on the title remained
- shadowOffset <== {0,0}, shadowColor <== nil (shadow not drawn according to docs)
******************/
if (&NSShadowAttributeName != NULL) {
// iOS6 methods
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:CGSizeZero];
[shadow setShadowColor:[UIColor clearColor]];
[titleTextAttributes setObject:shadow forKey:NSShadowAttributeName];
} else {
// Pre-iOS6 methods
[titleTextAttributes setValue:[UIColor clearColor] forKey:NSShadowAttributeName];
[titleTextAttributes setValue:[NSValue valueWithUIOffset:UIOffsetZero] forKey:NSShadowAttributeName];
}
[self setTitleTextAttributes:titleTextAttributes forState:controlState];
}
}
//helper method, basically a wrapper to allow creating a custom UIAppearance method that doesn't conform to the usual naming style
+ (void) configureItemOrProxy:(id)appearance
forFlatButtonWithColor:(UIColor *)color
highlightedColor:(UIColor *)highlightedColor
cornerRadius:(CGFloat) cornerRadius {
UIImage *backButtonPortraitImage = [UIImage backButtonImageWithColor:color
barMetrics:UIBarMetricsDefault
cornerRadius:cornerRadius];
UIImage *highlightedBackButtonPortraitImage = [UIImage backButtonImageWithColor:highlightedColor
barMetrics:UIBarMetricsDefault
cornerRadius:cornerRadius];
UIImage *backButtonLandscapeImage = [UIImage backButtonImageWithColor:color
barMetrics:UIBarMetricsLandscapePhone
cornerRadius:2];
UIImage *highlightedBackButtonLandscapeImage = [UIImage backButtonImageWithColor:highlightedColor
barMetrics:UIBarMetricsLandscapePhone
cornerRadius:2];
[appearance setBackButtonBackgroundImage:backButtonPortraitImage
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[appearance setBackButtonBackgroundImage:backButtonLandscapeImage
forState:UIControlStateNormal
barMetrics:UIBarMetricsLandscapePhone];
[appearance setBackButtonBackgroundImage:highlightedBackButtonPortraitImage
forState:UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];
[appearance setBackButtonBackgroundImage:highlightedBackButtonLandscapeImage
forState:UIControlStateHighlighted
barMetrics:UIBarMetricsLandscapePhone];
[appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(1.0f, 1.0f) forBarMetrics:UIBarMetricsDefault];
[appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(1.0f, 1.0f) forBarMetrics:UIBarMetricsLandscapePhone];
UIImage *buttonImageNormal = [UIImage imageWithColor:color cornerRadius:cornerRadius];
UIImage *buttonImageHightlighted = [UIImage imageWithColor:highlightedColor cornerRadius:cornerRadius];
[appearance setBackgroundImage:buttonImageNormal forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[appearance setBackgroundImage:buttonImageHightlighted forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
}
@end