Skip to content

Commit 4e35a49

Browse files
committed
演示如何为 tabBarItem添加选中背景
1 parent b6e8bb6 commit 4e35a49

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

Example/AppDelegate.m

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ - (void)customizeTabBarForController:(CYLTabBarController *)tabBarController {
102102

103103
- (void)customizeInterface {
104104
[self setUpNavigationBarAppearance];
105-
[self setUpTabBarItemTextAttributes];
105+
// [self setUpTabBarItemTextAttributes];
106106
}
107107
/**
108108
* 设置navigationBar样式
@@ -143,21 +143,60 @@ - (void)setUpNavigationBarAppearance {
143143
*/
144144
- (void)setUpTabBarItemTextAttributes {
145145

146+
147+
// set the text color for unselected state
146148
// 普通状态下的文字属性
147149
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
148-
normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
150+
normalAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
149151

152+
// set the text color for selected state
150153
// 选中状态下的文字属性
151154
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
152-
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
155+
selectedAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
153156

157+
// set the text Attributes
154158
// 设置文字属性
155159
UITabBarItem *tabBar = [UITabBarItem appearance];
156160
[tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
157161
[tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateHighlighted];
158162

163+
// Set the dark color to selected tab (the dimmed background)
164+
// TabBarItem选中后的背景颜色
165+
[[UITabBar appearance] setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor colorWithRed:26/255.0 green:163/255.0 blue:133/255.0 alpha:1] forSize:CGSizeMake([UIScreen mainScreen].bounds.size.width/5.0f, 49) withCornerRadius:0]];
166+
167+
// set the bar background color
159168
// 设置背景图片
160-
// UITabBar *tabBarAppearance = [UITabBar appearance];
161-
// [tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbar_background_os7"]];
169+
UITabBar *tabBarAppearance = [UITabBar appearance];
170+
[tabBarAppearance setBackgroundImage:[UIImage imageNamed:@"tabbar_background_os7"]];
171+
}
172+
173+
+ (UIImage *)imageFromColor:(UIColor *)color forSize:(CGSize)size withCornerRadius:(CGFloat)radius
174+
{
175+
CGRect rect = CGRectMake(0, 0, size.width, size.height);
176+
UIGraphicsBeginImageContext(rect.size);
177+
178+
CGContextRef context = UIGraphicsGetCurrentContext();
179+
CGContextSetFillColorWithColor(context, [color CGColor]);
180+
CGContextFillRect(context, rect);
181+
182+
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
183+
UIGraphicsEndImageContext();
184+
185+
// Begin a new image that will be the new image with the rounded corners
186+
// (here with the size of an UIImageView)
187+
UIGraphicsBeginImageContext(size);
188+
189+
// Add a clip before drawing anything, in the shape of an rounded rect
190+
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];
191+
// Draw your image
192+
[image drawInRect:rect];
193+
194+
// Get the image, here setting the UIImageView image
195+
image = UIGraphicsGetImageFromCurrentImageContext();
196+
197+
// Lets forget about that we were drawing
198+
UIGraphicsEndImageContext();
199+
200+
return image;
162201
}
163202
@end

Example/Classes/Main/View/CYLPlusButtonSubclass.m

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ +(void)load {
2020
[super registerSubclass];
2121
}
2222

23-
#pragma mark -
24-
#pragma mark - Life Cycle
25-
2623
-(instancetype)initWithFrame:(CGRect)frame{
2724

2825
if (self = [super initWithFrame:frame]) {
@@ -49,13 +46,12 @@ - (void)layoutSubviews
4946
// imageView 和 titleLabel 中心的 Y 值
5047
CGFloat const centerOfImageView = verticalMargin + imageViewEdge * 0.5;
5148
CGFloat const centerOfTitleLabel = imageViewEdge + verticalMargin * 2 + labelLineHeight * 0.5 + 5;
49+
5250
//imageView position 位置
53-
5451
self.imageView.bounds = CGRectMake(0, 0, imageViewEdge, imageViewEdge);
5552
self.imageView.center = CGPointMake(centerOfView, centerOfImageView);
5653

5754
//title position 位置
58-
5955
self.titleLabel.bounds = CGRectMake(0, 0, self.bounds.size.width, labelLineHeight);
6056
self.titleLabel.center = CGPointMake(centerOfView, centerOfTitleLabel);
6157
}
@@ -69,7 +65,7 @@ - (void)layoutSubviews
6965
*/
7066
+ (instancetype)plusButton{
7167

72-
CYLPlusButtonSubclass *button = [[CYLPlusButtonSubclass alloc]init];
68+
CYLPlusButtonSubclass *button = [[CYLPlusButtonSubclass alloc] init];
7369

7470
[button setImage:[UIImage imageNamed:@"post_normal"] forState:UIControlStateNormal];
7571
[button setTitle:@"发布" forState:UIControlStateNormal];
@@ -126,6 +122,8 @@ - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSIn
126122
NSLog(@"buttonIndex = %ld", buttonIndex);
127123
}
128124

125+
#pragma mark - CYLPlusButtonSubclassing
126+
129127
//+ (NSUInteger)indexOfPlusButtonInTabBar {
130128
// return 3;
131129
//}

0 commit comments

Comments
 (0)