1.UITabBarController的寫法
- (void)tab{
UITabBarController * tbc = [[UITabBarController alloc]init];
vc1 *v1 = [vc1 new];
v1.title = @"首頁";
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:v1];
UIImage *img1 = [UIImage imageNamed:@"tab1"];
v1.tabBarItem.image = img1;
vc2 *v2 = [vc2 new];
v2.title = @"附近";
vc3 *v3 = [vc3 new];
v3.title = @"搜索";
v3.tabBarItem.badgeValue = @"3";
vc4 *v4 = [vc4 new];
v4.title = @"設(shè)置";
NSArray *arr = @[nc,v2,v3,v4];
tbc.viewControllers = arr;
tbc.selectedIndex = 0;
[tbc.tabBar setTintColor:[UIColor greenColor]];
[tbc.tabBar setBarTintColor:[UIColor orangeColor]];
self.window.rootViewController = tbc;
}
2.StoryboardCode 起始程序TabBarController
UIStoryboard * mainstoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabbar = [mainstoryBoard instantiateInitialViewController];
//首頁故事板
UIStoryboard * faceStoryboard = [UIStoryboard storyboardWithName:@"Face" bundle:nil];
UINavigationController * faceNaVC = [faceStoryboard instantiateInitialViewController];
[faceNaVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"titleBar"] forBarMetrics:UIBarMetricsDefault];
faceNaVC.tabBarItem.image = [[UIImage imageNamed:@"dingdan_hui"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
faceNaVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"dingdan_lan"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[faceNaVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:RGBA(168, 168, 168, 1.0)} forState:UIControlStateNormal];
[faceNaVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:RGBA(0, 170, 239, 1.0)} forState:UIControlStateHighlighted];
FaceViewController * faceVC = [faceStoryboard instantiateViewControllerWithIdentifier:@"FaceViewController"];
3.頁面都是故事版
想要改變tabbar的圖片和文字
//tabbar顏色
[[UITabBar appearance] setBarTintColor:[UIColor redColor]];
//去除圖片原有藍(lán)色層
for (UITabBarItem *item in self.tabController.toolbarItems) {
item.selectedImage = [item.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
// 自定義文字顏色
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateSelected];