iOS-個(gè)人整理15 - 標(biāo)簽視圖控制器--UITabBarController

一、UITabBarController

微信底部那一排 微信跛锌、通訊錄、發(fā)現(xiàn)届惋、朋友圈髓帽,就是UITabBarController
它上面的控制的四個(gè)視圖控制器是平級(jí)的,一般情況下將self.window.rootViewController設(shè)置為UITabBarController
然后在UITabBarController上面添加UINavigationController
UINavigationController上面添加一般的UIViewController
UITabBarController-->UINavigationController-->UIViewController

二脑豹、屬性和方法

UITabBar的高度是49郑藏,最多顯示5個(gè)UITabBarItem,超過(guò)5個(gè)會(huì)增加一個(gè)更多按鈕瘩欺。

 #import "RootTabBarController.h" 
#import "SecondViewController.h" 

    //創(chuàng)建一個(gè)ViewController加到NavigationController上  
    //再將NavigationController加到TabBarController上  
  
    RootViewController *rootVC = [[RootViewController alloc]init]; 
 
    UINavigationController *navigationC = [[UINavigationController alloc]initWithRootViewController:rootVC];  
      
    //創(chuàng)建UITabBarController  
    UITabBarController *myTabBar = [UITabBarController new];  
      
    //將導(dǎo)航控制器放入tabBar  
    myTabBar.viewControllers = @[navigationC];  
      
    //設(shè)置底部的item標(biāo)題译秦,這個(gè)字很小  
    navigationC.tabBarItem.title = @"根";  
      
    //設(shè)置圖片,這里圖片大小不能自適應(yīng)击碗,最好把圖片裁好 40*40筑悴,照片還有設(shè)置渲染效果,背景色會(huì)影響照片顯示  
    navigationC.tabBarItem.image = [[UIImage imageNamed:@"b3.gif"]  
    imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;  
      
    //設(shè)置tabBar的字體顏色  
    myTabBar.tabBar.tintColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];  
      
    //設(shè)置tabbar背景顏色  
    myTabBar.tabBar.barTintColor =  [UIColor colorWithRed:200/255.0 green:233/255.0 blue:160/255.0 alpha:1];  
  
    //是否半透明  
    myTabBar.tabBar.translucent = NO;  
  
    //將tabBarController設(shè)置為根視圖控制器  
    self.window.rootViewController = myTabBar;  
      
    //把SecondView添加  
    SecondViewController *secondVC = [[SecondViewController alloc]init];  
    UINavigationController *secondNavC = [[UINavigationController alloc]initWithRootViewController:secondVC];  
      
    //設(shè)置標(biāo)題  
    secondNavC.tabBarItem.title = @"second";  
      
    //設(shè)置item角標(biāo)稍途,右上角的紅色數(shù)字阁吝,未讀消息數(shù)  
    secondNavC.tabBarItem.badgeValue = @"10";  
  
    //當(dāng)前被選中的頁(yè)面/按鈕    
    myTabBar.selectedIndex = 0;  
  
    //將第三個(gè)界面加入  
    tabbar ThirdViewController *thirdVC = [[ThirdViewController alloc]init];   
    UINavigationController *thirdNavC = [[UINavigationController alloc]initWithRootViewController:thirdVC];   
    thirdNavC.tabBarItem.title = @"third";   
    //將所有視圖加入  
    tabbar myTabBar.viewControllers = @[navigationC,secondNavC,thirdNavC];   
    //設(shè)置默認(rèn)頁(yè)   
    myTabBar.selectedIndex = 1;   
    //設(shè)置為根視圖控制器 RootTabBarViewController *rootTabBarVC = [[RootTabBarViewController alloc]init];   
    self.window.rootViewController = rootTabBarVC  

UITabBarController 自帶邏輯,點(diǎn)擊下面的分區(qū)bar會(huì)自動(dòng)跳轉(zhuǎn)到相應(yīng)的NavigationController下的ViewController

這是UITabBarItem的樣式和對(duì)應(yīng)的枚舉值


三械拍、自定義UITabBarViewController

系統(tǒng)的TabBar有時(shí)不能滿足我們的需求突勇,我們可以更改它的外觀。
自定義一個(gè)UIView的視圖坷虑,貼在UITabBar的View上
將UITabBarController的tabbar屬性的hidden設(shè)置為YES甲馋,就可以隱藏原有的tabbar顯示了。

先寫(xiě)自定義的tabBarUIview迄损,思路是做一個(gè)底部橫條視圖定躏,給上面加上自定義的button,給button加上方法芹敌,該方法來(lái)改變系統(tǒng)的UITabBarController的selectedIndex值痊远。
這里涉及一個(gè)頁(yè)面間傳值的問(wèn)題,可以使用block或者協(xié)議方法
customTabBarView.h

#import <UIKit/UIKit.h>  
  
//block傳值  
  
typedef void(^passValue)(NSInteger tag);  
  
@protocol CustomTabBarDelegate <NSObject>  
  
//把btn的tag傳出去的方法  
-(void)selectedIndexWithTag:(NSInteger)tag;  
  
@end  
  
@interface CustomTabBarView : UIView  
  
@property (nonatomic,copy)passValue passValueTag;  
@property (nonatomic,assign)id <CustomTabBarDelegate>delegate;  
  
@end  

customTabBarView.m


#import "CustomTabBarView.h"  
  
@implementation CustomTabBarView  
  
//創(chuàng)建按鈕  
-(void)createSubViews  
{  
      
    for (int i = 0; i < 2; i ++) {  
        //初始化按鈕  
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];  
        [btn setTitle:[@(i) stringValue] forState:UIControlStateNormal];  
          
        //點(diǎn)擊時(shí)的樣式  
        [btn setTitle:@"選" forState:UIControlStateHighlighted];  
        [btn setTitle:@"33" forState:UIControlStateFocused];  
        [btn setImage:[[UIImage imageNamed:@"b33"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];  
        [btn setTitle:@"" forState:UIControlStateSelected];  
          
        //選中狀態(tài)  
//        btn.selected = NO;  
          
        //通過(guò)tag值來(lái)確定我們點(diǎn)擊的是哪一個(gè)按鈕氏捞,來(lái)切換到對(duì)應(yīng)的視圖控制器  
        [btn setTag:1000+i];  
          
        //設(shè)置點(diǎn)擊方法  
        [btn addTarget:self action:@selector(customSelectedIndex:) forControlEvents:UIControlEventTouchDown];  
          
        //設(shè)置frame  
        btn.frame = CGRectMake((414)/2*i, 0, 414/2, 50);  
          
          
        [self addSubview:btn];  
    }  
}  
  
//按鈕的回調(diào)方法  
-(void)customSelectedIndex:(UIButton*)sender  
{  
    for (int  i = 0; i< 2; i++) {  
        UIButton *btn = (UIButton*)[self viewWithTag:1000+i];  
        btn.selected = NO;  
    }  
      
    sender.selected = YES;  
      
//    判斷在指定的代理類(lèi)中是否實(shí)現(xiàn)了該協(xié)議方法  
//    確保執(zhí)行時(shí)無(wú)此方法時(shí)不崩潰  
    if([self.delegate respondsToSelector:@selector(selectedIndexWithTag:)])  
    {  
        [self.delegate selectedIndexWithTag:(sender.tag - 1000)];  
    }  
    else  
    {  
        NSLog(@"selectIndexWithTag該方法沒(méi)有實(shí)現(xiàn)");  
    }  
      
    //調(diào)用block方法  
    //self.passValueTag(sender.tag - 1000);  
      
}  
  
//初始化自定義的UIView  
-(instancetype)initWithFrame:(CGRect)frame  
{  
    //設(shè)置frame  
    frame = CGRectMake(0, 736-50, 414, 50);  
      
    self = [super initWithFrame:frame];  
    if (self) {  
        [self createSubViews];  
        self.backgroundColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];  
    }  
      
    return self;  
}  
  
@end  

在RootTabBController.m中添加自定義的視圖碧聪,并隱藏原本的TabBar
這里的secondViewController和FirstViewController就是跳轉(zhuǎn)后顯示的VC

#import "RootTabBarController.h"  
#import "FirstViewController.h"  
#import "SecondViewController.h"  
#import "CustomTabBarView.h"  
  
//導(dǎo)入?yún)f(xié)議  
@interface RootTabBarController ()<CustomTabBarDelegate>  
  
@end  
  
@implementation RootTabBarController  
  
-(void)viewDidLoad  
{  
    [super viewDidLoad];  
      
    //創(chuàng)建視圖控制器  
    FirstViewController *firstVC = [[FirstViewController alloc]init];  
    UINavigationController *firstNavC = [[UINavigationController alloc]initWithRootViewController:firstVC];  
    firstVC.view.backgroundColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];  
      
    SecondViewController *secondVC = [[SecondViewController alloc]init];  
    UINavigationController *secondNavC = [[UINavigationController alloc]initWithRootViewController:secondVC];  
    secondVC.view.backgroundColor = [UIColor colorWithRed:200/255.0 green:233/255.0 blue:160/255.0 alpha:1];  
      
    //給tabBarController添加子控制器  
    [self addChildViewController:firstNavC];  
    [self addChildViewController:secondNavC];  
      
    //使用自定義的tabbar先把系統(tǒng)的隱藏掉  
    self.tabBar.hidden = YES;  
      
    //初始化自定義的tabbar  
    CustomTabBarView *customView = [[CustomTabBarView alloc]initWithFrame:CGRectZero];  
    [self.view addSubview:customView];  
      
    //指定代理  
    customView.delegate = self;  
      
    //設(shè)置block內(nèi)容  
    customView.passValueTag = ^(NSInteger tag)  
    {  
        self.selectedIndex = tag;  
    };     
}  
//實(shí)現(xiàn)代理方法  
- (void)selectedIndexWithTag:(NSInteger)tag  
{  
    self.selectedIndex = tag;  
}  
@end  

做的比較丑和簡(jiǎn)單


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市液茎,隨后出現(xiàn)的幾起案子逞姿,更是在濱河造成了極大的恐慌辞嗡,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件滞造,死亡現(xiàn)場(chǎng)離奇詭異欲间,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)断部,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)猎贴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人蝴光,你說(shuō)我怎么就攤上這事她渴。” “怎么了蔑祟?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵趁耗,是天一觀的道長(zhǎng)诲锹。 經(jīng)常有香客問(wèn)我蕾盯,道長(zhǎng),這世上最難降的妖魔是什么镰矿? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任径簿,我火速辦了婚禮罢屈,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘篇亭。我一直安慰自己缠捌,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布译蒂。 她就那樣靜靜地躺著曼月,像睡著了一般。 火紅的嫁衣襯著肌膚如雪柔昼。 梳的紋絲不亂的頭發(fā)上哑芹,一...
    開(kāi)封第一講書(shū)人閱讀 51,488評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音捕透,去河邊找鬼聪姿。 笑死,一個(gè)胖子當(dāng)著我的面吹牛激率,可吹牛的內(nèi)容都是我干的咳燕。 我是一名探鬼主播勿决,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼乒躺,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了低缩?” 一聲冷哼從身側(cè)響起嘉冒,我...
    開(kāi)封第一講書(shū)人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤曹货,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后讳推,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體顶籽,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年银觅,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了礼饱。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡究驴,死狀恐怖镊绪,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情洒忧,我是刑警寧澤蝴韭,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站熙侍,受9級(jí)特大地震影響榄鉴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蛉抓,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一庆尘、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧巷送,春花似錦减余、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至堡牡,卻和暖如春抒抬,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背晤柄。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工擦剑, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人芥颈。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓惠勒,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親爬坑。 傳聞我的和親對(duì)象是個(gè)殘疾皇子纠屋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • UITabBarController(標(biāo)簽欄/底部導(dǎo)航欄視圖控制器) UITabBarController 本身并...
    小小土豆dev閱讀 8,353評(píng)論 0 6
  • 前言的前言 唐巧前輩在微信公眾號(hào)「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各項(xiàng)指標(biāo)...
    VincentHK閱讀 5,363評(píng)論 3 44
  • 早上剛出門(mén)盾计,才發(fā)現(xiàn)售担,已經(jīng)是入秋了赁遗!只是葉子還沒(méi)有落,人卻已經(jīng)加衣了族铆。 以前的時(shí)候岩四,我是不懼怕寒冷的,可是不知從何起...
    林蔓谷閱讀 426評(píng)論 0 0
  • 我來(lái)到你的城?重慶 2017·5月·重慶 步游重慶【小確幸哥攘,微夢(mèng)想】 2016那些走過(guò)的路剖煌,看過(guò)的景?你好奇不 2...
    舒涵vivian閱讀 1,242評(píng)論 0 1
  • 引言: 一直想用這個(gè)題目很久了。折中了這么多年的思想逝淹,最后也還是喜歡鬼神文化所以帶來(lái)的神秘感末捣。喜歡吸血鬼伯爵,也喜...
    朱竹煮助閱讀 639評(píng)論 2 4