UIStatusBar iOS上狀態列的自訂色彩

來源:互聯網
上載者:User

標籤:

App啟動時狀態列控制App啟動的時候系統載入需要一定的時間,可以給App提供了Launch Image或Launch Screen以增強使用者體驗。在啟動頁顯示出來的時候App還沒有運行,也就談不上在程式中控制狀態列的字型顏色、顯示或隱藏。預設情況下狀態列是顯示出來的,並且Style為UIStatusBarStyleDefault,即黑色。1、隱藏可以在Info中將Status bar is initially hidden(UIStatusBarHidden)對應的Value設定為Yes。也可以在General中將Hide status bar勾選:實際上,上面兩種設定方法最終作用到info.plist檔案。可以直接修改該檔案,如果不嫌麻煩又不擔心出錯的話。如果沒有使用基於ViewController的狀態列控制,並且App內部又需要將狀態列顯示出來,可以在AppDelegate中設定:[[UIApplication sharedApplication] setStatusBarHidden:NO];2、設定字型顏色為白色可以在Info中將Status bar style(UIStatusBarStyle)對應的Value設定為UIStatusBarStyeLightContent。也可以在General中將Status Bar style選擇為Light:同樣的,上面兩種設定方法最終作用到info.plist檔案。如果沒有使用基於ViewController的狀態列控制,並且App內部又需要將狀態列顏色改為黑色,可以在AppDelegate中設定:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
App運行時狀態列控制建立一個Xcode項目,App預設是基於ViewController的狀態列控制,即在ViewController重載prefersStatusBarHidden、preferredStatusBarStyle和preferredStatusBarUpdateAnimation三個方法,及在必要時調用setNeedsStatusBarAppearanceUpdate方法。如果要使用iOS7之前的通過UIApplication控制狀態列,需在target的info.plist中增加一條View controller-based status bar appearance(UIViewControllerBasedStatusBarAppearance)並設定為NO。 1、View controller-based status bar appearance : YES 或 info.plist無此條目 
UIViewController方法 說明
- (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0); // Defaults to NO 詢問是否隱藏狀態列。
- (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0); // Defaults to UIStatusBarStyleDefault 詢問狀態列樣式(UIStatusBarStyleDefault/UIStatusBarStyleLightContent)。
// 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); // Defaults to UIStatusBarAnimationFade 詢問狀態列顯示或隱藏動畫。
// This should be called whenever the return values for the view controller‘s status bar attributes have changed. If it is called from within an animation block, the changes will be animated along with the rest of the animation block.- (void)setNeedsStatusBarAppearanceUpdate NS_AVAILABLE_IOS(7_0); 設定需要更新狀態列。主動調用該方法,將間接調用上述三個方法。如果需要動畫生效,需:    [UIView animateWithDuration:0.4
                     animations:^{
                         [self setNeedsStatusBarAppearanceUpdate];                     }];
   
 2、View controller-based status bar appearance : NO  
UIApplication方法/屬性 說明
// Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.@property(nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden;- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation NS_AVAILABLE_IOS(3_2); 設定是否隱藏狀態列。
// Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.@property(nonatomic) UIStatusBarStyle statusBarStyle; // default is UIStatusBarStyleDefault- (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle animated:(BOOL)animated; 設定狀態列樣式(UIStatusBarStyleDefault/UIStatusBarStyleLightContent)。
   
 如果要在App啟動時和運行時全程隱藏狀態列,在View controller-based status bar appearance為NO的情況下,只需簡單將Status bar is initially hidden(UIStatusBarHidden)設定為YES。 可以根據是否是基於ViewController的狀態列控制來決定是否調用UIApplication中控制狀態列的相關方法,也可以直接調用。因為在基於ViewController的狀態列控制時,調用UIApplication中控制狀態列的相關設定方法會被忽略。

 

UIStatusBar 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.