先搭建骨架,底部tabbar,切换控制器

先创建TabBarController
UIViewController *vc01 = [[UIViewController alloc] init];
vc01.tabBarItem.title = @"精华";
vc01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
UIImage * image = [UIImage imageNamed:@"tabBar_essence_click_icon"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc01.tabBarItem.selectedImage = image;
vc01.view.backgroundColor = [UIColor redColor];
[self addChildViewController:vc01];
UIViewController *vc02 = [[UIViewController alloc] init];
vc02.tabBarItem.title = @"新帖";
vc02.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
vc02.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_new_click_icon"];
vc02.view.backgroundColor = [UIColor orangeColor];
[self addChildViewController:vc02];
UIViewController *vc03 = [[UIViewController alloc] init];
vc03.tabBarItem.title = @"关注";
vc03.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
vc03.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_friendTrends_click_icon"];
vc03.view.backgroundColor = [UIColor yellowColor];
[self addChildViewController:vc03];
UIViewController *vc04 = [[UIViewController alloc] init];
vc04.tabBarItem.title = @"我";
vc04.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
vc04.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_me_click_icon"];
vc04.view.backgroundColor = [UIColor greenColor];
[self addChildViewController:vc04];
但是,有没有觉得有大量重复代码,显得很烂啊,稍等再精简一下下吧
由于苹果给tabbar渲染过效果,所以tabbar看起来是

与要求不符,要做修改
方式一
UIViewController *vc01 = [[UIViewController alloc] init];
vc01.tabBarItem.title = @"精华";
vc01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
UIImage * image = [UIImage imageNamed:@"tabBar_essence_click_icon"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//RenderingMode渲染模式 原始
vc01.tabBarItem.selectedImage = image;
vc01.view.backgroundColor = [UIColor redColor];
[self addChildViewController:vc01];
效果图

但是这种代码还是这么多,还得写四个,不想写啊。还有,如果以后这个图片用到其他地方也不想被渲染,就还得写一遍

好的,那么在找一个简单一点的方法吧,
如图,找到需要的图片选中

就好了

紧接着还有一个问题,( ⊙o⊙ )?文字怎么还是蓝色的?!既然设置文字和视图属性用tabbarItem,那么文字颜色呢
UIViewController *vc01 = [[UIViewController alloc] init];
vc01.tabBarItem.title = @"精华";
vc01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
[vc01.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
[vc01.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
vc01.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];
vc01.view.backgroundColor = [UIColor redColor];
[self addChildViewController:vc01];
文字颜色就可以改变了,但是还得写4个
[vc02.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
[vc02.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
[vc03.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
[vc03.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
[vc04.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
[vc04.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
所以要优化一下(一改全改)
进入方法中查看,其结尾有
UI_APPEARANCE_SELECTOR
凡是带有UI_APPEARANCE_SELECTOR宏的,都可以用另一种方法去设置,就是拿到APPEARANCE。那么先把上边01,02,03,04设置文字颜色的方法注释掉,
//通过appearance统一设置所有UITabBarItem的文字属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
//好处就是,只要给appearance设置,以后都是一个效果
UITabBarItem * item = [UITabBarItem appearance];//appearance外观
[item setTitleTextAttributes:attrs forState:(UIControlStateNormal)];
[item setTitleTextAttributes:selectedAttrs forState:(UIControlStateSelected)];
//必须结尾有UI_APPEARANCE_SELECTOR
网友评论