欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS-修改Tabbar选中时候默认渲染成蓝色的状态

程序员文章站 2022-06-01 22:17:47
...

背景

好像我记得iOS6的时候,tabbar里面的东西是不会自动渲染的,也就是说,你设置成怎样显示出来就是怎样。之后的版本tabbar会被渲染,包括文字和图片。默认状态不会被渲染,但是选中状态会被渲染成蓝色。
实际开发中,我们不想让他被渲染,下面讲讲解决渲染的办法。


解决图片渲染问题

找到你的图片在Xcode的位置,然后把Render As的选项修改成Original Image的选项。顾名思义,就是渲染效果是原始图片的效果,即不做渲染。

iOS-修改Tabbar选中时候默认渲染成蓝色的状态


解决文字渲染的问题

文字渲染,这个就需要写代码来解决了。当然了,解决图片渲染的问题,也可以通过代码来解决。

 NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    attributes[NSFontAttributeName] = [UIFont systemFontOfSize:11];

    NSMutableDictionary *selectAttri = [NSMutableDictionary dictionary];
    selectAttri[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    selectAttri[NSFontAttributeName] = [UIFont systemFontOfSize:11];
    //通过appearance对tabBarItem的文字属性进行统一设置,这样所有的控制的tabBarItem的文字属性久都是这种样式的了
    UITabBarItem *tabbar = [UITabBarItem appearance];
    [tabbar setTitleTextAttributes:attributes forState:UIControlStateNormal];
    [tabbar setTitleTextAttributes:selectAttri forState:UIControlStateSelected];

这种设置方法,是把tabbar的所有按钮状态全部改成一样的设置。如果你想点击不同按钮,出来的效果不一样,那么就分别设置每个控制器的tabBarItem。

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"];
相关标签: tabbar渲染