從 iOS 7 開始,navigationBar 的定制方式有了不少變化。下面把日常項目裡最常用到的幾類設置整理成速查表。
導航欄上所有按鈕的渲染色由 tintColor 統一控制:
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
返回按鈕的文字不屬於 B 控制器,而屬於即將返回到的那一頁,因此要在 A 中 push 之前設置:
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
backItem.title = @"你的文字";
self.navigationItem.backBarButtonItem = backItem;
// 右側文字按鈕
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"全部已讀"
style:UIBarButtonItemStylePlain
target:self
action:@selector(allReadedTapped)];
// 左側圖標按鈕
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backIcon"]
style:UIBarButtonItemStyleBordered
target:self
action:@selector(backButtonTouched:)];
標題的樣式通過 titleTextAttributes 字典配置:
self.title = @"登 錄";
self.navigationController.navigationBar.titleTextAttributes = @{
NSFontAttributeName: [UIFont systemFontOfSize:17],
NSForegroundColorAttributeName: [UIColor whiteColor]
};
背景色由 barTintColor 控制,注意它和 tintColor 分工不同:一個管底色,一個管按鈕前景色。
self.navigationController.navigationBar.barTintColor =
[UIColor colorFromHexRGB:@"3bad6f" alpha:1.0f];
AppDelegate 中統一配置 UINavigationBar 的外觀(iOS 7 起可用 appearance 代理對象);