iOS Status Bar 狀態(tài)欄設(shè)置匯總

總結(jié):

  • 1) Status bar is initially hidden為NO時(shí)View controller-based status bar appearance不生效
  • 2)View controller-based status bar appearance為NO時(shí)preferredStatusBarStyle和prefersStatusBarHidden方法不生效
  • 3)UIViewController 在 UINavigationController 導(dǎo)航欄中時(shí),在UIViewController中設(shè)置, preferredStatusBarStyle 不生效根本不會走,因?yàn)?UINavigationController 中也有 preferredStatusBarStyle 這個方法,會走UINavigationController,需要重寫UINavigationController中的方法方能生效(可以通過分類的方式或者baseNavigationde 方式)。

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

在 Info.plist 文件中添加

  • Status bar is initially hidden
    設(shè)置為 YES ,這個是隱藏 App 在 LunchScreen(歡迎界面)時(shí)的狀態(tài)欄。
    只隱藏LunchScreen的開啟頁面,其他頁面需要局部隱藏就通過prefersStatusBarHidden隱藏


    92bc9dbf545e7b67f9ec62d4ea0b0425.png
420b63fce1d794dd16e6bbfdaf313119.png
  • View controller-based status bar appearance
    設(shè)置為 NO這個是隱藏 App 在所有UIViewController 時(shí)的狀態(tài)欄?!井?dāng)然同時(shí)要項(xiàng)目勾選Hide status bar,這個沒有勾選,也不會隱藏】
    除了LunchScreen的開啟頁面不隱藏,其他頁面全部隱藏


    24054a7abf2b04f7653aa5f6e9763159.png
62746fbc2c6b8df82dd95f62aecb6269.png

特別注意

  • 1)當(dāng) Status bar is initially hidden 設(shè)置為 NO 的時(shí)候,不管 View controller-based status bar appearance 設(shè)置為 NO 還是 YES ,都是無效的。

  • 2)只有 Status bar is initially hidden 設(shè)置為 YES 的時(shí)候, 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;
// SwiftUI
Application.sharedApplication().statusBarHidden = true

特別注意:

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

3. 通過代碼實(shí)現(xiàn)狀態(tài)欄的局部隱藏

  • 上面的方法是全局隱藏,是隱藏 App 在所有 UIViewController 時(shí)的狀態(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 ,否則代碼不會有任何效果。

4、Status Bar 狀態(tài)欄的顏色

狀態(tài)欄分前后兩部分,要分清這兩個概念,后面會用到:

  • 文字部分:就是指的顯示電池、時(shí)間等部分。
  • 背景部分:就是顯示黑色或者圖片的背景部分。


    064e5f7c2dd043f31b53bdeb1f104cfd.png

    文字部分為白色,背景部分為黑色

1. 設(shè)置 Status Bar 的【文字部分】

簡單來說,就是設(shè)置顯示電池電量、時(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è)置兩個值,就是上面提到那兩個 UIStatusBarStyleDefault 和 UIStatusBarStyleLightContent 。


    cfa4d2fee2fa2089b75bc72a3f5c6c9e.png

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
}

但是!!當(dāng) UIViewController 在 UINavigationController 導(dǎo)航欄中時(shí),上面方法沒用, preferredStatusBarStyle 方法根本不會被調(diào)用,因?yàn)?UINavigationController 中也有 preferredStatusBarStyle 這個方法。

解決辦法有兩個:

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

@implementation MyNavigationController
- (UIStatusBarStyle)preferredStatusBarStyle {  
  UIViewController *topVC = self.topViewController;   
 return [topVC preferredStatusBarStyle];
}
@end

方法二: 自定義一個 UINavigationController 的子類,在這個子類中重寫 preferredStatusBarStyle 這個方法,這樣在 UIViewController 中就有效了,如下:

@implementation MyNavigationController
- (UIStatusBarStyle)preferredStatusBarStyle {  
  UIViewController *topVC = self.topViewController; 
   return [topVC preferredStatusBarStyle];
}
@end

方法三:全局設(shè)置UINavigationController的狀態(tài)欄,此方法同樣需要 Info.plist 文件中添加 View controller-based status bar appearance 設(shè)置為 YES

-(void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];

 [[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlackTranslucent];
  [self setNeedsStatusBarAppearanceUpdate]; 
}

- (void)viewWillDisappear:(BOOL)animated{
  [super viewWillDisappear:animated];
 [[[self navigationController] navigationBar] setBarStyle:UIBarStyleDefault];
[self setNeedsStatusBarAppearanceUpdate];
}

2. 設(shè)置 Status Bar 的【背景部分】

背景部分,簡單來說,就是狀態(tài)欄的背景顏色,其實(shí)系統(tǒng)狀態(tài)欄的背景顏色一直是透明的狀態(tài),當(dāng)有導(dǎo)航欄時(shí),導(dǎo)航欄背景是什么顏色,狀態(tài)欄就是什么顏色,沒有導(dǎo)航欄時(shí),狀態(tài)欄背后的視圖時(shí)什么顏色,它就是什么顏色。

// 這個方法是設(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; 
   }
}

[參考:]http://www.itdecent.cn/p/4166651dc64d

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

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

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