- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1. 建立視窗
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 2. 指定根控制器
UITabBarController *tabBarController = [[UITabBarController alloc] init];
self.window.rootViewController = tabBarController;
// 3. 顯示
[self.window makeKeyAndVisible];
return YES;
}
這個三步驟是所有 iOS 專案的起點:建窗、掛根控制器、顯示。順序不能顛倒,makeKeyAndVisible 之前必須已經有根控制器,否則畫面會是黑的。
LaunchImage 的詳細配置記錄在 LaunchImage.launchimage/Contents.json 裡,各尺寸對應的圖檔都寫在那份清單中。若要取消 App 圖示的系統渲染(保留原始色),需在 Target 的屬性中關閉對應選項。
另外,若希望程式啟動時先隱藏狀態列以呈現乾淨的開場,可在啟動階段設定隱藏;等載入完成後,於 didFinishLaunchingWithOptions 中呼叫 [application setStatusBarHidden:NO] 即可恢復。
自定義一個 TabBarViewController 繼承 UITabBarController,在它的 viewDidLoad 中建立各分頁。
- (void)viewDidLoad {
[super viewDidLoad];
UIViewController *home = [[UIViewController alloc] init];
home.view.backgroundColor = [UIColor redColor];
home.tabBarItem.title = @"首頁";
home.tabBarItem.image = [UIImage imageNamed:@"tabbar_home"];
[home.tabBarItem setSelectedImage:[UIImage imageNamed:@"tabbar_home_selected"]];
[self addChildViewController:home];
UIViewController *message = [[UIViewController alloc] init];
message.tabBarItem.title = @"消息";
message.tabBarItem.image = [UIImage imageNamed:@"tabbar_message_center"];
[message.tabBarItem setSelectedImage:[UIImage imageNamed:@"tabbar_message_center_selected"]];
[self addChildViewController:message];
// discover、profile 依此類推
}
iOS 7 起,系統預設會把 tabBarItem 的圖片重新渲染成主題藍色。若設計稿給的是彩色圖示,畫面就會全變成一片藍。解法是明確要求使用原圖:
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
但這個 API 只有 iOS 7 以上才有,因此必須搭配版本判斷,避免舊系統崩潰。
四個分頁的設定幾乎一樣,逐段複製顯得不專業。抽成一個方法:
- (void)addOneChildViewController:(UIViewController *)viewController
withTitle:(NSString *)title
imageName:(NSString *)imageName
selectedImageName:(NSString *)selectedImageName {
viewController.view.backgroundColor = ZFRandomColor;
viewController.tabBarItem.title = title;
viewController.tabBarItem.image = [UIImage imageNamed:imageName];
UIImage *image = [UIImage imageNamed:selectedImageName];
if (iOS7) {
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
[viewController.tabBarItem setSelectedImage:image];
[self addChildViewController:viewController];
}
之後新增分頁只要一行呼叫。ZFRandomColor 與 iOS7 這兩個自定義宏定義在 Prefix.pch 中:
#define ZFRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 \
green:arc4random_uniform(256)/255.0 \
blue:arc4random_uniform(256)/255.0 \
alpha:1.0]
#define iOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0)
為 UIImage 建立分類,讓「iOS 7 優先使用 _os7 素材」的邏輯只存在一處:
@implementation UIImage (Extension)
+ (UIImage *)imageWithName:(NSString *)imageName {
UIImage *image = nil;
if (iOS7) {
NSString *name = [imageName stringByAppendingString:@"_os7"];
image = [UIImage imageNamed:name];
}
if (!image) {
image = [UIImage imageNamed:imageName];
}
return image;
}
@end
這個分類之後會被專案裡所有載入圖片的地方使用,因此各頁不必再重複寫版本判斷。整體來說,這個階段最重要的觀念是:把「系統差異」與「重複邏輯」都收攏到少數幾個位置,後續的畫面開發才會輕鬆。