场景:特定页面需要修改状态栏的颜色
方法:可以利用view 的viewWillAppear 和 WillDisappear 分别设置和恢复,恢复的目的不影响其他页面
appear: [self setStatusBarBackgroundColor:UIColor.blackColor];
disappear: [self setStatusBarBackgroundColor:UIColor.clearColor];
- (void)setStatusBarBackgroundColor:(UIColor *)color
{
if (@available(iOS 13.0, *)) {
static UIView *statusBar = nil;
if (!statusBar) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
statusBar = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
[[UIApplication sharedApplication].keyWindow addSubview:statusBar];
statusBar.backgroundColor= color;
});
} else {
statusBar.backgroundColor= color;
}
} else {
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor= color;
}
}
}
网友评论