UIViewController *vc1 = [[UIViewController alloc] init];
vc1.tabBarItem.title = @"首页";
vc1.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
vc1.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];
给tabBarItem
设置好文字和图片后发现选中状态图片和文字都是蓝色的,并不是我们设置好的图片样式。那么如何修改图片文字选中变蓝以及文字大小呢?
修改图片变蓝
1.第一种方法,用代码修改
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.tabBarItem.title = @"首页";
vc1.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
//设置图片的渲染模式为原图original,然后在赋给tabBarItem
UIImage *image = [UIImage imageNamed:@"tabBar_essence_click_icon"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc1.tabBarItem.selectedImage = image;
2.第二种
DB926635-14C1-4395-8BCB-7A7658902F12.png
修改文字属性
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
//颜色属性
attributes[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
//字体大小属性
//还有一些其他属性的key可以去NSAttributedString.h文件里去找
attributes[NSFontAttributeName] = [UIFont systemFontOfSize:13];
NSMutableDictionary *selectAttri = [NSMutableDictionary dictionary];
selectAttri[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
selectAttri[NSFontAttributeName] = [UIFont systemFontOfSize:13];
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.tabBarItem.title = @"首页";
//设置为选中状态的文字属性
[vc1.tabBarItem setTitleTextAttributes:attributes forState:UIControlStateNormal];
//设置选中状态的属性
[vc1.tabBarItem setTitleTextAttributes:selectAttri forState:UIControlStateSelected];
vc1.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
vc1.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];
补充
进入setTitleTextAttributes
这个方法里可以看到这个方法后面有一个宏UI_APPEARANCE_SELECTOR
,只要后面有这个宏的方法都可以通过appearance统一设置。
就拿上面这个例子来说吧,我们可以通过appearance统一对文字的属性进行设置,这样就不用对每个vc的tabBarItem
设置文字的属性了。
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
attributes[NSFontAttributeName] = [UIFont systemFontOfSize:13];
NSMutableDictionary *selectAttri = [NSMutableDictionary dictionary];
selectAttri[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
selectAttri[NSFontAttributeName] = [UIFont systemFontOfSize:13];
//通过appearance对tabBarItem的文字属性进行统一设置,这样所有的控制的tabBarItem的文字属性久都是这种样式的了
UITabBarItem *tabbar = [UITabBarItem appearance];
[tabbar setTitleTextAttributes:attributes forState:UIControlStateNormal];
[tabbar setTitleTextAttributes:selectAttri forState:UIControlStateSelected];
网友评论