iOS修改状态条文字的颜色为白色的 正确方法

方案一. Info.plist中设置UIViewControllerBasedStatusBarAppearance 为NO时:

(1) 在info.plist中设置 Status bar style为UIStatusBarStyleLightContent或UIStatusBarStyleDefault有效, 所有页面默认为此样式

(2) 在需要改变状态栏颜色的ViewController中在ViewDidLoad方法中添加代码:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

方案二. Info.plist中设置UIViewControllerBasedStatusBarAppearance 为YES

(1) 在info.plist中设置 Status bar style为UIStatusBarStyleLightContent或UIStatusBarStyleDefault无效, 所有页面默认样式为UIStatusBarStyleDefault.

(2) 在根视图控制其中添加代码:

- (UIViewController *)childViewControllerForStatusBarStyle {
//    return self.topViewController;//栈顶的控制器 一般就是当前可见的控制器
    return self.visibleViewController;//当前可见的控制器
}

即状态栏样式由当前可见的控制器决定

(3) 在某控制器添加代码:

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;//UIStatusBarStyleDefault
}

只有完成(2)时, 此代码才有效

(4) 当某页面已经显示的时候需要动态地更改状态栏样式, 添加一下代码:

    [self setNeedsStatusBarAppearanceUpdate];

另外, 在使用模态视图跳转后, 状态栏会变成跳转后的页面设置的颜色, 而且返回后状态栏颜色不会改变. 例如UIAlertController的使用就是模态视图跳转, 我们需要在回收模块视图窗口后刷新状态栏:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action5 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self setNeedsStatusBarAppearanceUpdate];
    }];
    [alertController addAction:action5];
    [self presentViewController:alertController animated:true completion:nil];

参考资料:http://my.oschina.net/shede333/blog/304560
http://blog.csdn.net/ouyangtianhan/article/details/45893797

你可能感兴趣的:(iOS修改状态条文字的颜色为白色的 正确方法)