UITabBarController

1.認(rèn)識

1.UITabBarController和UINavigationController類似,UITabBarController也可以輕松地管理多個控制器,輕松完成控制器之間的切換,典型的例子就是QQ袒啼、微信等應(yīng)?灭衷。但是.前者其管理的視圖一直存在,而后者在pop后會銷毀掉,釋放內(nèi)存.
注意:UITabBarController通常作為整個程序的rootViewController,而且不能添加到別的container viewController中。

2.它是如下所示的視圖控制器六荒,有人叫它分欄視圖控制器服爷,也有人叫它選項(xiàng)卡控制器或頁簽視圖控制器(通常我不稱它為標(biāo)簽視圖控制器主要是為了避免和UILabel標(biāo)簽混淆)邑退,它是在很多App種都能見到的一種視圖控制器饿序,如下圖所示

使用UITabBarController的場景很多,下圖所示的微信和喜馬拉雅聽書都使用分欄視圖控制器

2.使用分析

  • 創(chuàng)建UITabBarController
// 創(chuàng)建分欄控制器
    UITabBarController * tbc = [[UITabBarController alloc] init];
    // 添加分欄控制器管理的視圖控制器
    tbc.viewControllers = @[視圖控制器1, 視圖控制器2, ... ];
    // 將UITabBarController對象作為根視圖控制器
    self.window.rootViewController = tbc;
  • UITabBarController的構(gòu)成:

和UINavigationController非常類似,UITabBarController也包含一個UITabBar(選項(xiàng)卡欄)皂林,上面有若個個UITabBarItem(選項(xiàng)卡項(xiàng))朗鸠,每個選項(xiàng)卡項(xiàng)又由標(biāo)題、圖片和徽章構(gòu)成

  • 將UINavigationController加入到UITabBarController中

UINavigationController和UITabBarController混合使用在App開發(fā)中還是很常見的础倍,大致有兩種使用模式:

1.UITabBarController各子界面是獨(dú)立的導(dǎo)航關(guān)系烛占,互不影響

    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@”FirstViewController” bundle:nil];
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@”SecondViewController” bundle:nil];
    UINavigationController* nav1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
    UINavigationController* nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
    UITabBarController* tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nil];
    self.window.rootViewController = tabBarController;
2.應(yīng)用整體是一個導(dǎo)航關(guān)系,只在根界面上分若干選項(xiàng)卡頁

    UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@”FirstViewController” bundle:nil];
    UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@”SecondViewController” bundle:nil];
    UITabBarController* tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:tabBarController];
    self.window.rootViewController = navController;
  • 多于4個分欄的處理:
    通常情況下分欄條最多支持5個視圖控制器沟启,如果超過5個忆家,只顯示4個,第5項(xiàng)變成了一個More美浦,點(diǎn)擊后會出現(xiàn)一個UITableViewController用于放置多余的視圖控制器弦赖,通過導(dǎo)航可以切換项栏,也可以對其進(jìn)行編輯浦辨,如下圖所示:
  • 顯示上次選中的視圖:

NSUserDefaults提供了 standardUserDefaults 類方法來獲得NSUserDefaults對象,接下來就是鍵值對映射的讀寫操作沼沈,使用起來非常的簡單

// 實(shí)現(xiàn)UITabBarControllerDelegate協(xié)議中的方法在選中某個視圖控制器時回調(diào)
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    // 將用戶選中的視圖控制器的索引通過NSUserDefaults對象存儲起來
    [userDef setInteger:tabBarController.selectedIndex forKey:@"selectedTabIndex"];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // ... ...

    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    // 將UITabBarController對象選中的索引設(shè)置為plist存儲的選中索引
    tbc.selectedIndex = [userDef integerForKey:@"selectedTabIndex"];

    // ... ...
}
  • 獲取UITabBarController中的所有子視圖控制器:

viewControllers屬性可以幫助我們獲得分欄視圖控制器上所有的子視圖控制器的數(shù)組流酬,我們可以通過下標(biāo)運(yùn)算來獲得某個子視圖控制器

  • UITabBar和UITabBarItem:
    1.圖片:selectedImage屬性
    2.徽章:badgeValue屬性
    3.顏色:tintColor屬性

3.UITabBarController的定制

如何定制一個喜馬拉雅聽書那樣的UITabBarController呢?我們可以繼承UITabBarController并隱去自帶的UITabBar列另,然后對整個下面的區(qū)域進(jìn)行完全定制芽腾。XIB的可視化效果和關(guān)鍵代碼如下所示:

#import "CDMyTabBarController.h"

@interface CDMyTabBarController () {
    UIView *bottomView;
}

@end

@implementation CDMyTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 隱去原有的分欄條
    self.tabBar.hidden = YES;
    // 通過XIB加載一個視圖替換原來的分欄條
    bottomView = [[[NSBundle mainBundle] loadNibNamed:@"CDMyTabBarView" owner:self options:nil] firstObject];

    CGRect rect = self.view.bounds;
    bottomView.frame = CGRectMake(0, rect.size.height - 92, rect.size.width, 92);

    [self.view addSubview:bottomView];

    for (UIView *tempView in bottomView.subviews) {
        if ([tempView isKindOfClass:[UIButton class]]) {
            UIButton *tempButton = (id)tempView;
            [tempButton addTarget:self action:@selector(tabBarButtonItemClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (void)tabBarButtonItemClicked:(UIButton *)sender {
    // 根據(jù)按鈕的tag值確定選中的分欄的索引
    self.selectedIndex = sender.tag - 200;

    // 將所有按鈕的設(shè)置為未選中狀態(tài)
    for (UIView *tempView in bottomView.subviews) {
        if ([tempView isKindOfClass:[UIButton class]]) {
            UIButton *tempButton = (id)tempView;
            tempButton.selected = NO;
        }
    }

    // 將點(diǎn)擊的按鈕設(shè)置為選中狀態(tài)
    sender.selected = YES;
}
@end

相關(guān)鏈接:
http://www.reibang.com/p/3dca06785c23
http://www.reibang.com/p/bedf090f1416

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市页衙,隨后出現(xiàn)的幾起案子摊滔,更是在濱河造成了極大的恐慌,老刑警劉巖店乐,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件艰躺,死亡現(xiàn)場離奇詭異,居然都是意外死亡眨八,警方通過查閱死者的電腦和手機(jī)腺兴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來廉侧,“玉大人页响,你說我怎么就攤上這事《翁埽” “怎么了闰蚕?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長连舍。 經(jīng)常有香客問我陪腌,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任诗鸭,我火速辦了婚禮染簇,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘强岸。我一直安慰自己锻弓,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布蝌箍。 她就那樣靜靜地躺著青灼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪妓盲。 梳的紋絲不亂的頭發(fā)上杂拨,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天,我揣著相機(jī)與錄音悯衬,去河邊找鬼弹沽。 笑死,一個胖子當(dāng)著我的面吹牛筋粗,可吹牛的內(nèi)容都是我干的策橘。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼娜亿,長吁一口氣:“原來是場噩夢啊……” “哼丽已!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起买决,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤沛婴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后督赤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嘁灯,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年够挂,在試婚紗的時候發(fā)現(xiàn)自己被綠了旁仿。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡孽糖,死狀恐怖枯冈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情办悟,我是刑警寧澤尘奏,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站病蛉,受9級特大地震影響炫加,放射性物質(zhì)發(fā)生泄漏瑰煎。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一俗孝、第九天 我趴在偏房一處隱蔽的房頂上張望酒甸。 院中可真熱鬧,春花似錦赋铝、人聲如沸插勤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽农尖。三九已至,卻和暖如春良哲,著一層夾襖步出監(jiān)牢的瞬間盛卡,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工筑凫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留滑沧,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓漏健,卻偏偏與公主長得像嚎货,于是被迫代替她去往敵國和親橘霎。 傳聞我的和親對象是個殘疾皇子蔫浆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評論 2 355

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