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 = @"设置";
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]];
//去除图片原有蓝色层
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];