【iOS 開發(fā)】Status Bar 狀態(tài)欄設(shè)置匯總

狀態(tài)欄

個人覺得 iOSStatus Bar 狀態(tài)欄也是一個比較坑的地方,所以還是寫一個總結(jié),有遇到這方面問題的朋友可以看一下。


Status Bar 狀態(tài)欄的隱藏

1. 通過設(shè)置 Info.plist 文件實(shí)現(xiàn)狀態(tài)欄的全局隱藏

  • Info.plist 文件中添加 Status bar is initially hidden 設(shè)置為 YES ,這個是隱藏 AppLunchScreen(歡迎界面)時的狀態(tài)欄。

  • Info.plist 文件中添加 View controller-based status bar appearance 設(shè)置為 NO,這個是隱藏 App 在所有 UIViewController 時的狀態(tài)欄。

Info.plist

特別注意:

當(dāng) 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. 通過代碼實(shí)現(xiàn)狀態(tài)欄的全局隱藏

  • Info.plist 文件中添加 View controller-based status bar appearance 設(shè)置為 NO 。

  • AppDelegate 文件中,實(shí)現(xiàn)下面方法(在其他 UIViewController 中也有效):

// OC
[UIApplication sharedApplication].statusBarHidden = YES;
  
// Swift
UIApplication.sharedApplication().statusBarHidden = true

特別注意:

如果想要通過代碼實(shí)現(xiàn)狀態(tài)欄隱藏,必須在 Info.plist 文件中添加 View controller-based status bar appearance ,并且必須設(shè)置為 NO ,否則代碼不會有任何效果,而且代碼只能隱藏 App 在所有 UIViewController 時的狀態(tài)欄,不能隱藏在 LunchScreen(歡迎界面)時的狀態(tài)欄。

3. 通過代碼實(shí)現(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
}

特別注意:

如果想要通過代碼實(shí)現(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)絡(luò)部分標(biāo)示的顏色, 這里只能設(shè)置兩種顏色:

// 默認(rèn)的黑色
UIStatusBarStyleDefault
  
// 白色
UIStatusBarStyleLightContent

1)通過設(shè)置 Info.plist 文件全局設(shè)置狀態(tài)欄的文字顏色

  • Info.plist 里增加一行 UIStatusBarStyle( Status bar style 也可以),這里可以設(shè)置兩個值,就是上面提到那兩個 UIStatusBarStyleDefaultUIStatusBarStyleLightContent 。
Info.plist

2)通過代碼全局設(shè)置狀態(tài)欄的文字顏色

  • Info.plist 文件中添加 View controller-based status bar appearance 設(shè)置為 NO (理論同上,必須添加且必須設(shè)置為 NO ,否則不生效)。

  • AppDelegate 文件中,實(shí)現(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
}

但是?。?/strong> 當(dāng) UIViewControllerUINavigationController 導(dǎo)航欄中時,上面方法沒用, preferredStatusBarStyle 方法根本不會被調(diào)用,因為 UINavigationController 中也有 preferredStatusBarStyle 這個方法。

解決辦法有兩個:

方法一: 設(shè)置導(dǎo)航欄的 barStyle 屬性會影響 status bar 的字體和背景色。如下。

// 狀態(tài)欄字體為白色,狀態(tài)欄和導(dǎo)航欄背景為黑色
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
// 狀態(tài)欄字體為黑色,狀態(tài)欄和導(dǎo)航欄背景為白色
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)欄的背景顏色,其實(shí)系統(tǒng)狀態(tài)欄的背景顏色一直是透明的狀態(tài),當(dāng)有導(dǎo)航欄時,導(dǎo)航欄背景是什么顏色,狀態(tài)欄就是什么顏色,沒有導(dǎo)航欄時,狀態(tài)欄背后的視圖時什么顏色,它就是什么顏色。

// 這個方法是設(shè)置導(dǎo)航欄背景顏色,狀態(tài)欄也會隨之變色
[self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];

如果想要單獨(dú)設(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;
    }
}

效果圖:

單獨(dú)設(shè)置狀態(tài)欄背景顏色

好了,關(guān)于 Status Bar 狀態(tài)欄的總結(jié)大概就這么多,其中說明了很多比較坑的細(xì)節(jié),網(wǎng)上很多資料都沒有說明清楚,希望對遇到這方面問題的朋友能有所幫助。

將來的你,一定會感激現(xiàn)在拼命的自己,愿自己與讀者的開發(fā)之路無限美好。

我的傳送門: 博客簡書 、微博GitHub 。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容