iOS開發(fā)之TabBarController使用篇

1.代碼創(chuàng)建TabBarController

demo下載:https://github.com/OwenJoe/UITabBarController_01.git

demo演示

1.AppDelegate 設(shè)置和代碼

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 設(shè)置和代碼

UITabBarController


 
    //初始化兩個視圖控制器
    OneViewController *oneVc = [[OneViewController alloc]init];
    TwoViewController *twoVc = [[TwoViewController alloc]init];
    
    //為兩個視圖控制器添加導航欄控制器
    UINavigationController *navOne = [[UINavigationController alloc]initWithRootViewController:oneVc];
    UINavigationController *navTwo = [[UINavigationController alloc]initWithRootViewController:twoVc];
    
    //設(shè)置控制器文字
    navOne.title = @"首頁";
    navTwo.title = @"個人中心";
    
    //設(shè)置控制器圖片(使用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的隱藏/顯示 設(shè)置和代碼

tabBar的隱藏式顯示
-(void)btnClickMethod{

    ThirdViewController *thirdVc = [[ThirdViewController alloc]init];
    //跳轉(zhuǎn)隱藏tabBar
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:thirdVc animated:YES];
    //返回時候顯示tabBar
    self.hidesBottomBarWhenPushed = NO;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容