Xcode9之前 設置狀態欄顏色首先在info.plist文件中,加入UIViewControllerBasedStatusBarAppearance = false; 讓後在delegate didFinishLaunchingWithOptions 方法中加入下麵的代碼就可以了; Xcode9之 ...
Xcode9之前
設置狀態欄顏色首先在info.plist文件中,加入UIViewControllerBasedStatusBarAppearance = false;
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
讓後在delegate didFinishLaunchingWithOptions 方法中加入下麵的代碼就可以了;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
Xcode9之後
xcode9.1我在設置狀態欄字體為白色時,按照上面的方法行不通,搜羅了一圈有了結果;
同樣在info.plist中,加入 UIViewControllerBasedStatusBarAppearance = true, 註意是true;
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
接著新建一個基於 UINavigationController 的類 BaseNavigationController,一個基於 UIViewController 類 BaseViewController ;
BaseNavigationController.m
-(UIViewController *)childViewControllerForStatusBarStyle { return self.topViewController; } -(UIViewController *)childViewControllerForStatusBarHidden { return self.topViewController; }
BaseViewController.m
-(UIStatusBarStyle)preferredStatusBarStyle{ ///這裡設置白色 return UIStatusBarStyleLightContent; }
-(BOOL)prefersStatusBarHidden{ return NO; }
在項目中如果新建UINavigationController就繼承BaseNavigationController,新建UIViewController就繼承BaseViewController,這樣就可以實現狀態欄字體改變了;如果你是已經有的現有項目,可以擴展UINavigationController和UIViewController,來進行實現;