效果
1.建一個(gè)類TabbarViewController繼承UITabBarController
- 封裝添加子控制器的方法
/**
* 封裝添加子控制器的方法
*
* @param childVc 控制器
* @param title 標(biāo)題
* @param image 正常狀態(tài)下的的圖標(biāo)
* @param selectedImage 選中狀態(tài)下的圖標(biāo)
*/
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
//設(shè)置標(biāo)題
childVc.tabBarItem.title = title;
childVc.tabBarItem.image = [UIImage imageNamed:image];
//需要設(shè)置照片的模式馍驯,用照片原圖,默認(rèn)是藍(lán)色的
childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//創(chuàng)建修改字體顏色的字典妆够,同時(shí)可以設(shè)置字體的內(nèi)邊距传趾;
// NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
// textAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:123/255 green:123/255 blue:123/255 alpha:1];
// [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
//
// NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
// selectTextAttrs[NSForegroundColorAttributeName] = [UIColor redColor];
// [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
//背景顏色
self.tabBar.barTintColor = [UIColor yellowColor];
//設(shè)置未選中的字體顏色
// if (@available(iOS 10.0, *)) {
// self.tabBar.unselectedItemTintColor = [UIColor blackColor];
// } else {
// // Fallback on earlier versions
// }
//選中時(shí)字體顏色
self.tabBar.tintColor = ThemeColor;
childVc.view.backgroundColor = [UIColor yellowColor];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:childVc];
//不要忘記添加到父控制器上
[self addChildViewController:nav];
}
3.添加控制器
- (void)viewDidLoad {
[super viewDidLoad];
HomeViewController *home = [[HomeViewController alloc] init];
[self addChildVc:home title:@"首頁(yè)" image:@"index_shop" selectedImage:@"index_shop_seleted"];
MessageCenterViewController *message = [[MessageCenterViewController alloc] init];
[self addChildVc:message title:@"消息" image:@"index_message" selectedImage:@"index_message_seleted"];
DiscoverViewController *discover = [[DiscoverViewController alloc] init];
[self addChildVc:discover title:@"發(fā)現(xiàn)" image:@"index_quanzi" selectedImage:@"index_quanzi_seleted"];
ProfileViewController *profile = [[ProfileViewController alloc] init];
[self addChildVc:profile title:@"我" image:@"index_my" selectedImage:@"index_my_seleted"];
}