改變 NavigationBar 返回按鈕的圖標
這個問題是這篇所謂問題中我花費時間最長才解決的,對于初學者的我真是一個不小的坑。
先說一下走過的彎路,一開始是通過繼承UIViewController(比如叫MyViewController),并設置其navigationItem
的屬性leftBarButtonItem
來實現的,而且必須要給這個按鈕實現點擊就返回的方法。
這樣做的缺點有:
- 所有放在 UINavigationController 中的 ViewController 都要繼承了這個 MyViewController
- 如果是在 UINavigationController 的棧底的 ViewController,需要增加一個隱藏返回按鈕的判斷
- 如果從 MyViewController 及其子類 push 了一個 iOS 的原生界面,比如 UIImagePickerController,那么這個返回按鈕就失效了
但是,終于讓我找到了一個更簡單并且解決以上所有缺點的方法,在 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,只好校正一下。
另外,如果想把返回按鈕的文字隱藏,我只找到了這么一個 workaround 的奇技淫巧:
[[UIBarButtonItem appearance]
setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
其實就是把按鈕文字向上移了60 point,并沒有隱藏,只是在屏幕上看不到而已,用 Reveal 還是可以看到……
改變 NavigationBar 的背景顏色
[UINavigationBar appearance].barTintColor = [UIColor blueColor];
改變 NavigationBar 的字體顏色
NavigationBar 上面有兩處可以改變字體顏色,一是標題,二是左右按鈕的文字。
改變按鈕的文字顏色:
[UINavigationBar appearance].tintColor = [UIColor whiteColor];
改變標題的文字顏色:
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
去掉 NavigationBar 下方的陰影
iOS 7 NavigationBar的下方默認是有一條陰影的,如果想要 NavigationBar 和下面內容的背景顏色融為一體的話,就要去掉這個陰影:
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[UIImage new]];
改變 TabBar 的字體顏色
[UITabBarItem.appearance setTitleTextAttributes:
@{ NSForegroundColorAttributeName : [UIColor blueColor] }
forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:
@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
forState:UIControlStateSelected];
改變 StatusBar 的顏色
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;
隱藏 StatusBar
有時候為了實現沉浸式設計,比如 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;
本文為博主原創,轉載請保留鏈接,謝謝