1.代碼創(chuàng)建TabBarController
demo下載:https://github.com/OwenJoe/UITabBarController_01.git
demo演示
1.AppDelegate 設置和代碼
AppDelegate
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//創(chuàng)建并初始化UITabBarController
WBTabBarController *tabBarController = [[WBTabBarController alloc]init];
self.window.rootViewController = tabBarController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
2.UITabBarController 設置和代碼
UITabBarController
//初始化兩個視圖控制器
OneViewController *oneVc = [[OneViewController alloc]init];
TwoViewController *twoVc = [[TwoViewController alloc]init];
//為兩個視圖控制器添加導航欄控制器
UINavigationController *navOne = [[UINavigationController alloc]initWithRootViewController:oneVc];
UINavigationController *navTwo = [[UINavigationController alloc]initWithRootViewController:twoVc];
//設置控制器文字
navOne.title = @"首頁";
navTwo.title = @"個人中心";
//設置控制器圖片(使用imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal,不被系統(tǒng)渲染成藍色)
navOne.tabBarItem.image = [[UIImage imageNamed:@"icon_home_bottom_statist"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navOne.tabBarItem.selectedImage = [[UIImage imageNamed:@"icon_home_bottom_statist_hl"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navTwo.tabBarItem.image = [[UIImage imageNamed:@"icon_home_bottom_search"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navTwo.tabBarItem.selectedImage = [[UIImage imageNamed:@"icon_home_bottom_search_hl"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//改變tabbarController 文字選中顏色(默認渲染為藍色)
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:[UIColor lightGrayColor]} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
//創(chuàng)建一個數(shù)組包含四個導航欄控制器
NSArray *vcArry = [NSArray arrayWithObjects:navOne,navTwo,nil];
//將數(shù)組傳給UITabBarController
self.viewControllers = vcArry;
3.tabBar的隱藏/顯示 設置和代碼
tabBar的隱藏式顯示
-(void)btnClickMethod{
ThirdViewController *thirdVc = [[ThirdViewController alloc]init];
//跳轉隱藏tabBar
self.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:thirdVc animated:YES];
//返回時候顯示tabBar
self.hidesBottomBarWhenPushed = NO;
}