你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios7中UIViewControllerBasedStatusBarAppearance作用詳解

ios7中UIViewControllerBasedStatusBarAppearance作用詳解

編輯:IOS開發綜合

  ios7中UIViewControllerBasedStatusBarAppearance詳解

在作iOS7的適配時,很多文章都會提到UIViewControllerBasedStatusBarAppearance。便一直不是太明白其實際作用,使用時發現UIViewControllerBasedStatusBarAppearance的實際作用如下:      

這個屬性只影響如何設置status bar上字體的顏色是暗色(黑色)還是亮色(白色),對status bar的背景色無影響。status bar的背景色在iOS7上永遠是透明的。

apple官方對UIViewControllerBasedStatusBarAppearance得說明:

UIViewControllerBasedStatusBarAppearance (Boolean - iOS) specifies whether the status bar appearance is based on the style preferred by the view controller that is currently under the status bar. When this key is not present or its value is set to YES, the view controller determines the status bar style. When the key is set to NO, view controllers (or the app) must each set the status bar style explicitly using the UIApplication object.

即:UIViewControllerBasedStatusBarAppearance(一個Boolean值)指定狀態欄的外觀是否是基於當前視圖控制器給狀態欄指定的首選風格。當這個鍵不存在,或者它的值設置為YES時(也就是說這個key的默認value為yes),視圖控制器決定了狀態欄的風格。當按鍵被設置為NO,視圖控制器(或應用程序)都必須通過UIApplication對象(即UIApplication的setStatusBarStyle方法)顯示的設置狀態欄風格。

通過apple的官方文檔不難看出,在ios7上,設置狀態欄風格(暗色或者亮色)的方法無非就兩種,第一種是在controller中通過回調方法preferredStatusBarStyle返回狀態欄的風格,第二種是通過UIApplication對象的setStatusBarStyle設置, UIViewControllerBasedStatusBarAppearance實際上是指定了是否優先使用第一種方法(這也就不難理解為什麼這個key值叫做UIViewControlle “based”)。

所以當plist中沒有UIViewControllerBasedStatusBarAppearance這個key,或者存在這個key,並且value為YES時,
viewController的preferredStatusBarStyle方法對狀態欄的設置生效;

當UIViewControllerBasedStatusBarAppearance對應的value為NO時,
[UIApplication sharedApplication] 通過方法setStatusBarStyle對狀態欄的設置生效。

隱藏狀態欄

有時候我們需要隱藏狀態欄,那麼此時我們在view controller中override方法prefersStatusBarHidden:即可,如下代碼所示:

- (BOOL)prefersStatusBarHidden 
{ 
  return YES; 
} 

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved