這個問題是這篇所謂問題中我花費時間最長才解決的,對於初學者的我真是一個不小的坑。
先說一下走過的彎路,一開始是經過繼承UIViewController(好比叫MyViewController),並設置其navigationItem
的屬性leftBarButtonItem
來實現的,並且必需要給這個按鈕實現點擊就返回的方法。
這樣作的缺點有:web
全部放在 UINavigationController 中的 ViewController 都要繼承了這個 MyViewControllerapp
若是是在 UINavigationController 的棧底的 ViewController,須要增長一個隱藏返回按鈕的判斷字體
若是從 MyViewController 及其子類 push 了一個 iOS 的原生界面,好比 UIImagePickerController,那麼這個返回按鈕就失效了spa
可是,終於讓我找到了一個更簡單而且解決以上全部缺點的方法,在 AppDelegate 中進行全局設置,代碼以下:設計
UIImage *backImage = [[[UIImage imageNamed:@"navigation_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, 11.5, 0)]; [[UINavigationBar appearance] setBackIndicatorImage:backImage]; [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backImage];
注:返回按鈕的圖片我採用的是44*44 point 的圖片,可是不知道爲何若是直接設置就會偏上11.5 point,只好校訂一下。code
另外,若是想把返回按鈕的文字隱藏,我只找到了這麼一個 workaround 的奇技淫巧:orm
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
其實就是把按鈕文字向上移了60 point,並無隱藏,只是在屏幕上看不到而已,用 Reveal 仍是能夠看到……繼承
[UINavigationBar appearance].barTintColor = [UIColor blueColor];
NavigationBar 上面有兩處能夠改變字體顏色,一是標題,二是左右按鈕的文字。圖片
[UINavigationBar appearance].tintColor = [UIColor whiteColor];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
iOS 7 NavigationBar的下方默認是有一條陰影的,若是想要 NavigationBar 和下面內容的背景顏色融爲一體的話,就要去掉這個陰影:ci
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setShadowImage:[UIImage new]];
[UITabBarItem.appearance setTitleTextAttributes: @{ NSForegroundColorAttributeName : [UIColor blueColor] } forState:UIControlStateNormal]; [UITabBarItem.appearance setTitleTextAttributes: @{ NSForegroundColorAttributeName : [UIColor whiteColor] } forState:UIControlStateSelected];
iOS7之後,status bar 的背景顏色變成了透明色,並且系統會根據 app的顏色自動改變 status bar 的字體顏色(黑和白)。可是這個自動改變的字體顏色並不必定和全部的 app 都搭配,好比咱們 app 的主題色是稍微淺一丟丟的藍,可是系統匹配的 status bar 的字體顏色就是黑色,看起來就很不爽,因此就要強制將其改成白色。
首先在 Info.plist 中的 Information Property List 中添加一個 Key爲View controller-based status bar appearance
的 item,其 Type 設爲 Boolean,Value 設爲 NO
而後在AppDelegate.m
的application:didFinishLaunchingWithOptions:
中添加:[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
有時候爲了實現沉浸式設計,好比 app 首次打開的引導頁,須要隱藏整個 StatusBar,方法以下:
和改變 StatusBar 顏色同樣,在 Info.plist 中的 Information Property List 中添加一個 Key爲View controller-based status bar appearance
的 item,其 Type 設爲 Boolean,Value 設爲 NO
在須要隱藏StatusBar 的 ViewController 中的viewDidLoad
加入如下代碼:
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { [self prefersStatusBarHidden]; [self setNeedsStatusBarAppearanceUpdate]; }
重寫prefersStatusBarHidden
:
-(BOOL)prefersStatusBarHidden { return YES; }
注:可是這樣設置的話從這個頁面開始整個 app 的 StatusBar 都會隱藏,若是想要再顯示出來,只須要在其餘 ViewController 中加入:
[UIApplication sharedApplication].statusBarHidden = NO;