iOS 设置状态栏背景颜色
iOS 13后 禁止使用 KVC ,导致原来设置状态栏颜色的发失效,新方法在网上找了一圈发现都是一样的失效,后来发现 是要先添加 navgationController 后,在设置以下代码,实测iOS13下可以改变状态栏背景颜色
```
- (void)setStatusBarBackgroundColor:(UIColor *)color {
if(@available(iOS13.0, *)) {
staticUIView* statusBar =nil;
if(!statusBar) {
UIWindow*keyWindow = [UIApplicationsharedApplication].windows[0];
statusBar = [[UIViewalloc]initWithFrame:keyWindow.windowScene.statusBarManager.statusBarFrame];
[keyWindowaddSubview:statusBar];
[statusBarsetBackgroundColor:color];
}else{
[statusBarsetBackgroundColor:color];
}
}else{
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if([statusBarrespondsToSelector:@selector(setBackgroundColor:)]) {
[statusBarsetBackgroundColor:color];
}
}
}
```
把这段代码卸载当前控制器里
```
- (void)viewDidLoad {
[super viewDidLoad];
[self.viewaddSubview:self.wkWebView];
self setStatusBarBackgroundColor:[UIColor colorWithRed:47.0/255.0 green:131.0/255.0 blue:252.0/255.0 alpha:1.0]];
}
```
新建项目如果没有添加 UINavigationController直接设置是不起作用的
```
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
MainViewController * viewC = [[MainViewController alloc]init];
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:viewC];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
```
最后不要忘记在plist文件里面设置View controller-based status bar appearance 为NO