幾乎每個中型以上的 App 都會用到「底部標籤欄 + 每個分頁各自一組導航堆疊」的結構。本文示範如何把兩者組裝起來,並處理首次啟動時的引導流程與各版本的樣式差異。
- (void)initTabBar {
// 首頁
HomePageViewController *homePage = [[HomePageViewController alloc] init];
UINavigationController *homePageNav = [[UINavigationController alloc] initWithRootViewController:homePage];
// 若使用者尚未選擇城市,直接推入選擇頁
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
if (![userDefault objectForKey:@"cityDic"]) {
CitySelectionViewController *citySelection = [[CitySelectionViewController alloc] init];
[homePageNav pushViewController:citySelection animated:YES];
}
UITabBarItem *itemHomePage = [[UITabBarItem alloc] initWithTitle:@"首頁"
image:[UIImage imageWithImage:[UIImage imageNamed:@"1-1.png"]
scaledToSize:CGSizeMake(25, 25)]
tag:0];
[itemHomePage setFinishedSelectedImage:[UIImage imageWithImage:[UIImage imageNamed:@"1-1.jpg"]
scaledToSize:CGSizeMake(25, 25)]
withFinishedUnselectedImage:[UIImage imageWithImage:[UIImage imageNamed:@"1-2.jpg"]
scaledToSize:CGSizeMake(25, 25)]];
homePageNav.tabBarItem = itemHomePage;
[itemHomePage setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
NAVBACKGROUNDCOLOR, UITextAttributeTextColor, nil]
forState:UIControlStateSelected];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO([NSString stringWithFormat:@"7.0"])) {
[homePageNav.navigationBar setBarTintColor:NAVBACKGROUNDCOLOR];
} else {
[homePageNav.navigationBar setTintColor:NAVBACKGROUNDCOLOR];
}
homePageNav.navigationBar.translucent = NO;
// 購物車
ShoppingCartViewController *shoppingCart = [[ShoppingCartViewController alloc] init];
UINavigationController *shoppingCartNav = [[UINavigationController alloc] initWithRootViewController:shoppingCart];
UITabBarItem *itemShoppingCart = [[UITabBarItem alloc] initWithTitle:@"我的購物車"
image:[UIImage imageWithImage:[UIImage imageNamed:@"1-1.png"]
scaledToSize:CGSizeMake(25, 25)]
tag:0];
[itemShoppingCart setFinishedSelectedImage:[UIImage imageWithImage:[UIImage imageNamed:@"1-1.jpg"]
scaledToSize:CGSizeMake(25, 25)]
withFinishedUnselectedImage:[UIImage imageWithImage:[UIImage imageNamed:@"1-2.jpg"]
scaledToSize:CGSizeMake(25, 25)]];
shoppingCartNav.tabBarItem = itemShoppingCart;
[itemShoppingCart setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
NAVBACKGROUNDCOLOR, UITextAttributeTextColor, nil]
forState:UIControlStateSelected];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO([NSString stringWithFormat:@"7.0"])) {
[shoppingCartNav.navigationBar setBarTintColor:NAVBACKGROUNDCOLOR];
} else {
[shoppingCartNav.navigationBar setTintColor:NAVBACKGROUNDCOLOR];
}
shoppingCartNav.navigationBar.translucent = NO;
// 個人中心
IndividualCenterViewController *individualCenter = [[IndividualCenterViewController alloc] init];
UINavigationController *individualCenterNav = [[UINavigationController alloc] initWithRootViewController:individualCenter];
UITabBarItem *itemIndividualCenter = [[UITabBarItem alloc] initWithTitle:@"我的幸福家"
image:[UIImage imageWithImage:[UIImage imageNamed:@"1-1.png"]
scaledToSize:CGSizeMake(25, 25)]
tag:0];
[itemIndividualCenter setFinishedSelectedImage:[UIImage imageWithImage:[UIImage imageNamed:@"1-1.jpg"]
scaledToSize:CGSizeMake(25, 25)]
withFinishedUnselectedImage:[UIImage imageWithImage:[UIImage imageNamed:@"1-2.jpg"]
scaledToSize:CGSizeMake(25, 25)]];
individualCenterNav.tabBarItem = itemIndividualCenter;
[itemIndividualCenter setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
NAVBACKGROUNDCOLOR, UITextAttributeTextColor, nil]
forState:UIControlStateSelected];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO([NSString stringWithFormat:@"7.0"])) {
[individualCenterNav.navigationBar setBarTintColor:NAVBACKGROUNDCOLOR];
} else {
[individualCenterNav.navigationBar setTintColor:NAVBACKGROUNDCOLOR];
}
individualCenterNav.navigationBar.translucent = NO;
// 組裝 TabBarController
self.tabBar = [[UITabBarController alloc] init];
[[UITabBar appearance] setBackgroundImage:
[UIImage setBackgroundImageByColor:COLOR(255, 255, 255, 1)
withFrame:RectWithPara(0, 0, SCREEN_WIDTH, 44)]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageWithTint:COLOR(235, 235, 235, 1)]];
self.tabBar.viewControllers = [NSArray arrayWithObjects:homePageNav,
shoppingCartNav,
individualCenterNav, nil];
}在 App 啟動時把 tabBar 設為 rootViewController:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self initTabBar];
self.window.rootViewController = self.tabBar;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}為什麼每個分頁都要各自包一層 UINavigationController?因為 UITabBarController 本身只負責「切換畫面」,不提供堆疊能力。若不包一層,使用者就無法在第二層頁面時用返回鍵回到上一頁,也無法在導航列上放標題與工具列。TabBarController 的 viewControllers 陣列裡放的,應該永遠是各分頁的「導航控制器」,而不是分頁的內容控制器本身。
TabBarItem 的圖片處理。這裡用 initWithTitle:image:tag: 建立項目,再用 setFinishedSelectedImage:withFinishedUnselectedImage: 分別指定選取與未選取狀態的圖。這組 API 在 iOS 7 之後已不再建議使用,新專案請直接改用 selectedImage: 並搭配 UIImageRenderingModeAlwaysOriginal,或乾脆使用 image: 加上 tintColor 讓系統自動上色。
首次啟動的引導流程。範例在建立首頁導航控制器之後,立刻判斷 NSUserDefaults 裡有沒有城市資料,若沒有就 push 選擇頁。這個做法能確保使用者一進 App 就完成必要設定,但也提醒一件事:請確認此時 homePageNav 已經被加入 viewControllers 陣列或至少已完成初始化,否則 push 的畫面可能不會如預期呈現。
translucent 的設定。把 navigationBar.translucent 設為 NO 可以讓內容不被導航列半透明遮擋,版面計算單純許多。若設定為 YES,就要記得為 ScrollView 設定 contentInsetAdjustmentBehavior,避免內容被導航列蓋住。