標(biāo)簽欄控制器是容器視圖控制器,專門(mén)用來(lái)管理視圖控制器捡絮。管理的視圖控制器之間是平級(jí)的熬芜。
使用的時(shí)要注意:我們一般會(huì)使用標(biāo)簽欄控制器去管理導(dǎo)航控制器,但是不會(huì)使用導(dǎo)航控制器去管理標(biāo)簽欄控制器福稳;導(dǎo)航控制器不能去管理導(dǎo)航控制器涎拉。
目錄
- 定制tabBar
- tabBarItem定制
定制tabBar
- 定制tabBar需要拿到標(biāo)簽欄控制器(任何可以拿到標(biāo)簽欄控制器的地方都可以來(lái)定制)
1.設(shè)置是否有透明度
[tabBarController.tabBar setTranslucent:NO];
2.設(shè)置tabBar的顏色
[tabBarController.tabBar setBarTintColor:[UIColor yellowColor]];
3.設(shè)置填充顏色(鏤空部分的顏色 -- 標(biāo)簽的文字的選中顏色)
[tabBarController.tabBar setTintColor:[UIColor redColor]];
4.設(shè)置背景圖片
[tabBarController.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbg"]];
5.在需要的時(shí)候隱藏tabBar
self.tabBarController.tabBar.hidden = YES;
tabBarItem定制
第一種方式定制tabBarItem
(1)設(shè)置標(biāo)簽正常狀態(tài)的圖片(如果選中圖片和沒(méi)有選中的圖片的樣式是一樣,只是顏色不一樣,只需呀設(shè)置tabBar的填充顏色就可以了鼓拧,可以不用去設(shè)置選中的圖片)
[nav1.tabBarItem setImage:[UIImage imageNamed:@"image"]];
(2)設(shè)置標(biāo)簽選中狀態(tài)的圖片
[nav1.tabBarItem setSelectedImage:[[UIImage imageNamed:@"faxian_d"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
(3)設(shè)置標(biāo)簽名
[nav1.tabBarItem setTitle:@"發(fā)現(xiàn)"];
(4)給不同狀態(tài)設(shè)置標(biāo)簽字體和顏色
[nav1.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17], NSForegroundColorAttributeName:[UIColor greenColor]} forState:UIControlStateNormal];
//字體只有設(shè)置正常狀態(tài)的字體有半火,選中狀態(tài)的字體和正常狀態(tài)下的字體一樣
[nav1.tabBarItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13], NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
(5)同時(shí)設(shè)置所有的標(biāo)簽的字體和顏色(如果已經(jīng)單獨(dú)設(shè)置過(guò)字體和顏色后,使用這個(gè)方法再去設(shè)置就無(wú)效)
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13], NSForegroundColorAttributeName:[UIColor purpleColor]} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13], NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];
6.將視圖控制器對(duì)象交給標(biāo)簽欄控制器去管理
//添加單個(gè)NavigationController
[tabBar addChildViewController:vc];
//添加多個(gè)NavigationController
tabBarC.viewControllers = @[nav1, nav2, nav3, nav4, nav5];
7.選中的標(biāo)簽下標(biāo)(默認(rèn)是0)
//(可以獲取也可以修改當(dāng)前選中的下標(biāo)的值)
tabBarC.selectedIndex = 1;
8.將標(biāo)簽欄控制器作為window的根視圖控制器
self.window.rootViewController = tabBarC;
第二種方法.
- 獲取到tabBar上顯示的所有的item季俩,然后設(shè)置對(duì)應(yīng)的屬性(已經(jīng)將需要標(biāo)簽欄控制管理的視圖控制交給標(biāo)簽欄控制器管理后去完成)
(1)獲取所有的tabBarItem
UITabBarController * tabBarController = (UITabBarController *)_window.rootViewController;
NSArray * tabBaritems = tabBarController.tabBar.items;
```
設(shè)置第三個(gè)item的屬性
```objc
UITabBarItem * item = tabBaritems[2];
//設(shè)置正常狀態(tài)的圖片
[item setImage:[UIImage imageNamed:@"zhuanti_u"]];
[item setSelectedImage:[[UIImage imageNamed:@"zhuanti_d"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
常用小方法
獲取根視圖控制器钮糖,TabBarController
//獲取根視圖控制器,TabBarController
ZBTabBarController *tabBarC = (ZBTabBarController *)[[UIApplication sharedApplication].keyWindow rootViewController];
修改tabBar選中的視圖&獲取tabBar的子視圖控制器
//獲取根視圖控制器
ZBTabBarController *tabBarC = (ZBTabBarController *)[[UIApplication sharedApplication].keyWindow rootViewController];
//修改tabBar選中的視圖
tabBarC.selectedViewController = tabBarC.viewControllers[0];
//獲取tabBar的子視圖控制器
UINavigationController *na = tabBarC.viewControllers[0]酌住;