共计 2967 个字符,预计需要花费 8 分钟才能阅读完成。
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];// 红色
}