
UITabBarController 本身也是 view controller,內部持有 viewControllers 陣列,每個元素就是一個分頁的內容控制器,每個分頁的標題與圖示則由各自的 tabBarItem 決定。
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
#pragma mark - 第一個分頁
HMT_AViewController *tabBarViewA = [[HMT_AViewController alloc] init];
tabBarViewA.tabBarItem.title = @"微信"; // 標籤欄標題
// tabBarViewA.tabBarItem.image = [UIImage imageNamed:@"1.png"]; // 自訂圖示
tabBarViewA.tabBarItem.badgeValue = @"3"; // 右上角提示,像未讀訊息數
#pragma mark - 第二個分頁
HMT_BViewController *tabBarViewB = [[HMT_BViewController alloc] init];
// 直接用系統提供的樣式(圖示與文字一體),UITabBarSystemItem 是列舉值
tabBarViewB.tabBarItem = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
tabBarViewB.tabBarItem.badgeValue = @"GO";
#pragma mark - 其餘分頁
HMT_CViewController *tabBarViewC = [[HMT_CViewController alloc] init];
tabBarViewC.tabBarItem = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:2];
tabBarViewC.tabBarItem.badgeValue = @"new";
HMT_DViewController *tabBarViewD = [[HMT_DViewController alloc] init];
tabBarViewD.tabBarItem = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
tabBarViewD.tabBarItem.badgeValue = @"99";
HMT_EViewController *tabBarViewE = [[HMT_EViewController alloc] init];
tabBarViewE.tabBarItem = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemHistory tag:4];
tabBarViewE.tabBarItem.badgeValue = @"sky";
#pragma mark - 第六個分頁
/* 系統預設能直接顯示的分頁上限是 5 個。
若 viewControllers 放進超過五個 items,tab bar controller 會自動插入一個
名為 More view controller 的特殊控制器,負責管理多餘的 items。
它以 table 的形式呈現剩餘項目,且數量不限 */
HMT_FViewController *tabBarViewF = [[HMT_FViewController alloc] init];
tabBarViewF.tabBarItem = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemContacts tag:5];
tabBarViewF.tabBarItem.badgeValue = @"AG";
#pragma mark - 建立 TabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
// 預設置底,若要調整位置可修改 frame(44 是 iPhone 上標準高度)
// tabBarController.tabBar.frame = CGRectMake(0, 20, 320, 44);
tabBarController.viewControllers = [NSArray arrayWithObjects:
tabBarViewA, tabBarViewB, tabBarViewC,
tabBarViewD, tabBarViewE, tabBarViewF, nil];
tabBarController.tabBar.tintColor = [UIColor greenColor];
tabBarController.tabBar.selectedImageTintColor = [UIColor brownColor];
// tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"@@@@@"];
// 啟動時預設顯示第 4 個分頁(索引從 0 起算)
tabBarController.selectedIndex = 3;
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}

幾個實務重點:badgeValue 是 NSString 型別,可放數字也可放文字;用系統內建的 UITabBarSystemItem 可以省去找圖的麻煩;而一旦分頁超過五個,More 機制就會自動接手。
先讓類別遵守 UITabBarControllerDelegate 協定,並設定 delegate:
tabBarController.delegate = self;
// 決定哪些分頁可以被點擊
- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController {
// HMT_CViewController 對應的分頁無法點擊
if ([viewController isKindOfClass:[HMT_CViewController class]]) {
return NO;
}
return YES;
}
// 分頁被選中時的回呼,可作為監聽用途
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController {
}
// More 頁面的編輯狀態:即將開始、即將結束、已結束
- (void)tabBarController:(UITabBarController *)tabBarController
willBeginCustomizingViewControllers:(NSArray *)viewControllers {
}
- (void)tabBarController:(UITabBarController *)tabBarController
willEndCustomizingViewControllers:(NSArray *)viewControllers
changed:(BOOL)changed {
}
- (void)tabBarController:(UITabBarController *)tabBarController
didEndCustomizingViewControllers:(NSArray *)viewControllers
changed:(BOOL)changed {
}
分頁控制器並不妨礙你在某個分頁內再疊上一層模態畫面。第一個分頁中放一顆按鈕觸發:
@implementation HMT_AViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = CGRectMake(100, 100, 100, 100);
[button addTarget:self action:@selector(modalShow)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)modalShow {
HMTModalShowViewController *modalShowVC = [[HMTModalShowViewController alloc] init];
// 呈現時的視覺效果
modalShowVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
/*
UIModalTransitionStyleCoverVertical 預設,由下往上
UIModalTransitionStyleFlipHorizontal 水平翻轉
UIModalTransitionStyleCrossDissolve 漸變
UIModalTransitionStylePartialCurl 書頁翻動
*/
// 呈現方式,預設全螢幕
modalShowVC.modalPresentationStyle = UIModalPresentationFullScreen;
/*
UIModalPresentationFullScreen = 0,
UIModalPresentationPageSheet,
UIModalPresentationFormSheet,
UIModalPresentationCurrentContext,
UIModalPresentationCustom,
UIModalPresentationNone = -1,
*/
UINavigationController *modalShowNC =
[[UINavigationController alloc] initWithRootViewController:modalShowVC];
[self presentViewController:modalShowNC animated:YES completion:^{
NSLog(@"hello world");
}];
}
@end
被呈現的那一層,用導航列上的按鈕關閉自己:
@implementation HMTModalShowViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
// 以 UINavigationController 的導航列作為關閉入口
UIBarButtonItem *barButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(modalDismiss)];
self.navigationItem.leftBarButtonItem = barButton;
self.navigationItem.title = @"humingtao";
}
- (void)modalDismiss {
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"退出GoodBye");
}];
}
@end
把這些組合起來,就是最典型的 iOS 主幹結構:UITabBarController 負責一級分頁切換,每個分頁各自套一個 UINavigationController 負責頁面堆疊,需要打斷流程時再視情況模態呈現新畫面。理解了這三層的分工,後續無論加多少功能,都能維持清楚的結構。