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