iOS開發(fā)教程之Status Bar狀態(tài)欄設(shè)置的方法匯總
前言
我們在很多app中可以看到不同與導航欄的狀態(tài)欄的顏色,比如下面:

狀態(tài)欄
個人覺得 iOS 的 Status Bar 狀態(tài)欄也是一個比較坑的地方,所以還是寫一個總結(jié),有遇到這方面問題的朋友可以看一下。
下面話不多說了,來隨著小編一起學習學習吧
Status Bar 狀態(tài)欄的隱藏
1. 通過設(shè)置 Info.plist 文件實現(xiàn)狀態(tài)欄的全局隱藏
在 Info.plist 文件中添加 Status bar is initially hidden 設(shè)置為 YES ,這個是隱藏 App 在 LunchScreen(歡迎界面)時的狀態(tài)欄。
在 Info.plist 文件中添加 View controller-based status bar appearance 設(shè)置為 NO,這個是隱藏 App 在所有 UIViewController 時的狀態(tài)欄。

Info.plist
特別注意:
當 Status bar is initially hidden 設(shè)置為 NO 的時候,不管 View controller-based status bar appearance 設(shè)置為 NO 還是 YES ,都是無效的,只有 Status bar is initially hidden 設(shè)置為 YES 的時候, View controller-based status bar appearance 才生效,這個要注意一下。
2. 通過代碼實現(xiàn)狀態(tài)欄的全局隱藏
在 Info.plist 文件中添加 View controller-based status bar appearance 設(shè)置為 NO 。
在 AppDelegate 文件中,實現(xiàn)下面方法(在其他 UIViewController 中也有效):
// OC [UIApplication sharedApplication].statusBarHidden = YES; // Swift UIApplication.sharedApplication().statusBarHidden = true
特別注意:
如果想要通過代碼實現(xiàn)狀態(tài)欄隱藏,必須在 Info.plist 文件中添加 View controller-based status bar appearance ,并且必須設(shè)置為 NO ,否則代碼不會有任何效果,而且代碼只能隱藏 App 在所有 UIViewController 時的狀態(tài)欄,不能隱藏在 LunchScreen(歡迎界面)時的狀態(tài)欄。
3. 通過代碼實現(xiàn)狀態(tài)欄的局部隱藏
上面的方法是全局隱藏,是隱藏 App 在所有 UIViewController 時的狀態(tài)欄,下面的方法是局部隱藏,是單個 UIViewController 內(nèi)的隱藏。
在 Info.plist 文件中添加 View controller-based status bar appearance 設(shè)置為 YES 。
在需要隱藏狀態(tài)欄的 UIViewController 文件中,加入下面方法:
// OC
- (BOOL)prefersStatusBarHidden {
return YES;
}
// Swift
override func prefersStatusBarHidden() -> Bool {
return true
}
特別注意:
如果想要通過代碼實現(xiàn)某個 UIViewController 狀態(tài)欄局部隱藏,必須在 Info.plist 文件中添加 View controller-based status bar appearance ,并且必須設(shè)置為 YES ,否則代碼不會有任何效果。
Status Bar 狀態(tài)欄的顏色
狀態(tài)欄分前后兩部分,要分清這兩個概念,后面會用到:
- 文字部分:就是指的顯示電池、時間等部分。
- 背景部分:就是顯示黑色或者圖片的背景部分。

文字部分為白色,背景部分為黑色
1. 設(shè)置 Status Bar 的【文字部分】
簡單來說,就是設(shè)置顯示電池電量、時間、網(wǎng)絡部分標示的顏色, 這里只能設(shè)置兩種顏色:
// 默認的黑色 UIStatusBarStyleDefault // 白色 UIStatusBarStyleLightContent
1)通過設(shè)置 Info.plist 文件全局設(shè)置狀態(tài)欄的文字顏色
在 Info.plist 里增加一行 UIStatusBarStyle( Status bar style 也可以),這里可以設(shè)置兩個值,就是上面提到那兩個 UIStatusBarStyleDefault 和 UIStatusBarStyleLightContent 。

Info.plist
2)通過代碼全局設(shè)置狀態(tài)欄的文字顏色
在 Info.plist 文件中添加 View controller-based status bar appearance 設(shè)置為 NO (理論同上,必須添加且必須設(shè)置為 NO ,否則不生效)。
在 AppDelegate 文件中,實現(xiàn)下面方法(在其他 UIViewController 中也有效):
// OC [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; // Swift UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
3)通過代碼局部設(shè)置狀態(tài)欄的文字顏色
在 Info.plist 文件中添加 View controller-based status bar appearance 設(shè)置為 YES (理論同上,必須添加且必須設(shè)置為 YES ,否則不生效) 。
在需要設(shè)置狀態(tài)欄顏色的 UIViewController 文件中,加入下面方法:
// OC
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
// Swift
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
但是!! 當 UIViewController 在 UINavigationController 導航欄中時,上面方法沒用, preferredStatusBarStyle 方法根本不會被調(diào)用,因為 UINavigationController 中也有 preferredStatusBarStyle 這個方法。
解決辦法有兩個:
方法一: 設(shè)置導航欄的 barStyle 屬性會影響 status bar 的字體和背景色。如下。
// 狀態(tài)欄字體為白色,狀態(tài)欄和導航欄背景為黑色 self.navigationController.navigationBar.barStyle = UIBarStyleBlack; // 狀態(tài)欄字體為黑色,狀態(tài)欄和導航欄背景為白色 self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
方法二: 自定義一個 UINavigationController 的子類,在這個子類中重寫 preferredStatusBarStyle 這個方法,這樣在 UIViewController 中就有效了,如下:
@implementation MyNavigationController
- (UIStatusBarStyle)preferredStatusBarStyle {
UIViewController *topVC = self.topViewController;
return [topVC preferredStatusBarStyle];
}
@end
2. 設(shè)置 Status Bar 的【背景部分】
背景部分,簡單來說,就是狀態(tài)欄的背景顏色,其實系統(tǒng)狀態(tài)欄的背景顏色一直是透明的狀態(tài),當有導航欄時,導航欄背景是什么顏色,狀態(tài)欄就是什么顏色,沒有導航欄時,狀態(tài)欄背后的視圖時什么顏色,它就是什么顏色。
// 這個方法是設(shè)置導航欄背景顏色,狀態(tài)欄也會隨之變色 [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
如果想要單獨設(shè)置狀態(tài)欄顏色,可以添加以下方法來設(shè)置:
/**
設(shè)置狀態(tài)欄背景顏色
@param color 設(shè)置顏色
*/
- (void)setStatusBarBackgroundColor:(UIColor *)color {
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
效果圖:

單獨設(shè)置狀態(tài)欄背景顏色
好了,關(guān)于 Status Bar 狀態(tài)欄的總結(jié)大概就這么多,其中說明了很多比較坑的細節(jié),網(wǎng)上很多資料都沒有說明清楚,希望對遇到這方面問題的朋友能有所幫助。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS 中 使用UITextField格式化銀行卡號碼的解決方案
今天小編給大家分享ios中使用UITextField格式化銀行卡號碼的實現(xiàn)思路詳解,非常不錯,具有參考借鑒價值,需要的朋友參考下2016-12-12
為textView添加語音輸入功能的實例代碼(集成訊飛語音識別)
下面小編就為大家分享一篇為textView添加語音輸入功能的實例代碼(集成訊飛語音識別),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01

