深入理解iOS的狀態列_IOS

來源:互聯網
上載者:User

一、狀態列的隱藏

狀態列的隱藏主要有兩種方法,下面來一起看看吧。

方法一:通過代碼控制

@interface UIApplication(UIApplicationDeprecated)// Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.@property(readwrite, nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden NS_DEPRECATED_IOS(2_0, 9_0, "Use -[UIViewController prefersStatusBarHidden]") __TVOS_PROHIBITED;- (void)setStatusBarHidden:(BOOL)hidden animated:(BOOL)animated NS_DEPRECATED_IOS(2_0, 3_2) __TVOS_PROHIBITED; // use -setStatusBarHidden:withAnimation:- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation NS_DEPRECATED_IOS(3_2, 9_0, "Use -[UIViewController prefersStatusBarHidden]") __TVOS_PROHIBITED;

注意:讓我們先來看看// Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.這個注釋提示,蘋果提示開發人員如果使用的是系統基礎的狀態列樣式你的這些設定是不生效的,在接下來要介紹的通過Info.plist隱藏狀態列同樣要注意這件事。

Info.plist中添加一個View controller-based status bar appearance設定選項,設定為NO這樣就可以使用上邊的方法了

注意:添加的View controller-based status bar appearanceBool類型,預設為Yes,很不幸iOS9之後蘋果已經不推薦使用這些方法了,這些方法能用但是會警示告。

那麼這些方法被禁用以後,如何操作呢?注釋裡已經提示Use -[UIViewController prefersStatusBarHidden]這是iOS7之後蘋果在UIViewController裡添加的新方法,這麼做的目的可以讓開發人員更加靈活的自訂每個ViewController的狀態列。

- (BOOL)prefersStatusBarHidden{  return YES;}

iOS7之後UIViewController中不只提供了這個關於狀態列的設定的函數,還有其他的,後面詳細說。

方法二:通過Info.plist控制

1,首先我們依然要設定這個(第2步中的兩種方式都要設定這個參數)

2,然後設定(兩種方式)

或者

兩者是等效的!並且兩者的狀態是同步的。

二、狀態列樣式

先看看都有哪些樣式(解釋看注釋)

typedef NS_ENUM(NSInteger, UIStatusBarStyle) {//預設樣式,黑字透明狀態列,適合用於背景色為亮色的頁面  UIStatusBarStyleDefault                   = 0, // Dark content, for use on light backgrounds//白字透明狀態列,適合用於背景色為暗色的頁面  UIStatusBarStyleLightContent   NS_ENUM_AVAILABLE_IOS(7_0) = 1, // Light content, for use on dark backgrounds// iOS7.0以前黑底白字,iOS7以後跟UIStatusBarStyleLightContent效果一樣  UIStatusBarStyleBlackTranslucent NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 1,// iOS7.0以前啟動頁為灰底白字,iOS7以後跟UIStatusBarStyleLightContent效果一樣  UIStatusBarStyleBlackOpaque   NS_ENUM_DEPRECATED_IOS(2_0, 7_0, "Use UIStatusBarStyleLightContent") = 2,} __TVOS_PROHIBITED;

如何設定狀態列樣式

// Setting the statusBarStyle does nothing if your application is using the default UIViewController-based status bar system.@property(readwrite, nonatomic) UIStatusBarStyle statusBarStyle NS_DEPRECATED_IOS(2_0, 9_0, "Use -[UIViewController preferredStatusBarStyle]") __TVOS_PROHIBITED;- (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle animated:(BOOL)animated NS_DEPRECATED_IOS(2_0, 9_0, "Use -[UIViewController preferredStatusBarStyle]") __TVOS_PROHIBITED;

同樣iOS9以後這些方法被禁用了,蘋果推薦在具體的viewControllerUse -[UIViewController preferredStatusBarStyle]

- (UIStatusBarStyle)preferredStatusBarStyle{  return UIStatusBarStyleLightContent;}

注意:我們通常使用的viewController都是嵌套在UINavigationController中使用的,此時在viewController中使用- (UIStatusBarStyle)preferredStatusBarStyle;函數會發現設定並沒有生效。

系統也給我們提供了一個函數- (UIViewController *)childViewControllerForStatusBarStyle,也可以解決這個問題,後面會講。

三、背景色

iOS7以後預設情況下狀態列的背景為透明的,一種辦法是我們自己寫一個UIView作為背景添加到狀態列下面,這樣就可以隨意設定狀態列的顏色了。

另一種方法就是通過設定navigationBarsetBarTintColor顏色來改變狀態列顏色

UIViewController中其他有關狀態列的函數

preferredStatusBarUpdateAnimation函數

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to UIStatusBarAnimationFade

如果想在當前已經顯示的UIViewController中更改狀態列的樣式的話,需要調用以上函數。調用該函數後,系統會主動調用preferredStatusBarStyle方法重繪狀態列的樣式

childViewControllerForStatusBarStyle函數

// Override to return a child view controller or nil. If non-nil, that view controller's status bar appearance attributes will be used. If nil, self is used. Whenever the return values from these methods change, -setNeedsUpdatedStatusBarAttributes should be called.- (nullable UIViewController *)childViewControllerForStatusBarStyle NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;

這個函數的傳回值預設返回nil,此時系統就會調用當前viewControllerApreferredStatusBarStyle函數;如果傳回值是另一個viewControllerB那麼系統就會調用viewControllerBpreferredStatusBarStyle函數。

運用這個函數就可以解決嵌套UINavigationController設定樣式無效的問題。

解釋一下為什麼嵌套UINavigationControllerviewControllerpreferredStatusBarStyle函數設定無效:

在我們嵌套了UINavigationController的時候,我們的AppDelegate.window.rootViewController通常是我們建立的navigationController,這時首先會調用的是navigationController中的childViewControllerForStatusBarStyle函數,因為預設返回nil,那麼接下來就會調用navigationController本身的preferredStatusBarStyle函數,所以我們在viewController中通過preferredStatusBarStyle函數設定的狀態列樣式就不會被調用發現,所以也就無效了。

所以我們要自己建立一個繼承於UINavigationcontrollerNavigationController,在這個子類中重寫childViewControllerForStatusBarStyle函數

- (UIViewController *)childViewControllerForStatusBarStyle{  return self.topViewController;}

這樣navigationController中的childViewControllerForStatusBarStyle函數會返回navigationController中最上層的viewController,那麼viewController中的preferredStatusBarStyle函數的設定就會被系統獲知

childViewControllerForStatusBarHidden函數

- (nullable UIViewController *)childViewControllerForStatusBarHidden NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;

childViewControllerForStatusBarHidden函數的使用原理同上,不再贅述。

preferredStatusBarUpdateAnimation函數

// Override to return the type of animation that should be used for status bar changes for this view controller. This currently only affects changes to prefersStatusBarHidden.- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to UIStatusBarAnimationFade

動畫形式如下

typedef NS_ENUM(NSInteger, UIStatusBarAnimation) {  UIStatusBarAnimationNone,  UIStatusBarAnimationFade NS_ENUM_AVAILABLE_IOS(3_2),  UIStatusBarAnimationSlide NS_ENUM_AVAILABLE_IOS(3_2),} __TVOS_PROHIBITED;

這個函數返回了動畫效果。動畫效果只有在prefersStatusBarHidden函數傳回值變化的時候才會展示,同時要通過調用
[self setNeedsStatusBarAppearanceUpdate]函數來重繪狀態列

四、應用

我們可以通過隱藏系統狀態列,然後自訂UIWindow通過設定setWindowLevel:UIWindowLevelStatusBar實現自訂狀態列。

總結

以上就是這篇文章的全部內容了,希望能對各位iOS開發人員們有所協助,如果有疑問大家可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.