iOS-状态栏字体颜色【白色】【Xcode9.1】

时间:2023-06-07 22:56:48

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,来进行实现;