1

2019.12.04 17:26

info.plist 属性View controller-based status bar appearance, 也就是UIViewControllerBasedStatusBarAppearance, 官方文档说明如下:

A Boolean value indicating whether the status bar appearance is based on the style preferred for the current view controller.

一个布尔值,指示状态栏外观是否基于当前视图控制器的首选样式。

状态栏字体颜色

View controller-based status bar appearance设置为 YES 时, NO 状态对应设置无效; 反之亦然.

方法一: preferredStatusBarStyle

View controller-based status bar appearance 设置为 YES, 使用 - [UIViewController preferredStatusBarStyle] 修改状态栏字体色. 适用于iOS7.0+, 推荐.

// MyViewController.m

- (UIStatusBarStyle)preferredStatusBarStyle {
      // 白色
      //return UIStatusBarStyleLightContent;
    
      // 黑色
      if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleDarkContent;
    } else {
        return UIStatusBarStyleDefault; //黑色, 默认值
    }
}

- (BOOL)prefersStatusBarHidden {
    return YES;  //状态栏隐藏
    //return NO; //状态栏显示, 默认值
}

注意:

若 ViewController 是 UINavigationController 的子控制器时, UINavigationController 的 preferredStatusBarStyle 方法覆盖了 ViewController preferredStatusBarStyle , 导致上述代码无效.

解决办法之一: 自定义导航栏, 重写 preferredStatusBarStyle 指定到当前顶层 viewController 的 preferredStatusBarStyle.

@implementation MyNavigationController
- (UIStatusBarStyle)preferredStatusBarStyle {
 UIViewController *topVC = self.topViewController;
 return [topVC preferredStatusBarStyle];
}
@end

方法二: setStatusBarStyle:

View controller-based status bar appearance 设置为 NO, 使用 [[UIApplication sharedApplication] setStatusBarStyle:] 改变导航栏字体颜色. 不推荐这种方式, 官方 API 也推荐使用-[UIViewController preferredStatusBarStyle]:

官方 API:

// Setting the statusBarStyle does nothing if your application is using the default UIViewController-based status bar system.
@property(readwrite, nonatomic) UIStatusBarStyle statusBarStyle API_DEPRECATED("Use -[UIViewController preferredStatusBarStyle]", ios(2.0, 9.0)) API_UNAVAILABLE(tvos);
- (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle animated:(BOOL)animated API_DEPRECATED("Use -[UIViewController preferredStatusBarStyle]", ios(2.0, 9.0)) API_UNAVAILABLE(tvos);

// Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.
@property(readwrite, nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden API_DEPRECATED("Use -[UIViewController prefersStatusBarHidden]", ios(2.0, 9.0)) API_UNAVAILABLE(tvos);
- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation API_DEPRECATED("Use -[UIViewController prefersStatusBarHidden]", ios(3.2, 9.0)) API_UNAVAILABLE(tvos);

方式 1

在 Xcode : General > Deployment Info > Status Bar Style 设置全局样式 light/dark, 这里的设置会被方式 2 的代码覆盖.

方式 2

AppDelegate中可以设置整个App的状态栏全局样式:

// AppDelegate.m

//设置状态栏字体颜色
[[UIApplication sharedApplication] setStatusBarStyle:[self _statusBarStyle]];
//设置状态栏隐藏(YES)或显示(NO)
[[UIApplication sharedApplication] setStatusBarHidden:NO];

- (UIStatusBarStyle)_statusBarStyle {
      // 设置字体黑色
    if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleDarkContent; // iOS13+
    } else {
        return UIStatusBarStyleDefault;
    }
}

改变指定页面状态栏的字体颜色需要单独设置:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:[self _statusBarStyle] animated:NO];// 黑色
      //[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];// 白色
}

Jerod
47 声望4 粉丝