iOS 13之后,苹果禁止KVC直接修改私有属性。以前用KVC修改状态栏背景色也会Crash。
新的解决办法如下。
if (@available(iOS 13.0, *)) {
UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
statusBar.backgroundColor = color;
[[UIApplication sharedApplication].keyWindow addSubview:statusBar];
} else {
// Fallback on earlier versions
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
swift
if #available(iOS 13.0, *) {
let statusBar : UIView = UIView.init(frame: UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.init(x: 0, y: 0, width: kScreenW, height: 20))
statusBar.backgroundColor = color
UIApplication.shared.keyWindow?.addSubview(statusBar)
} else {
// Fallback on earlier versions
let statusBarWindow : UIView = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView
let statusBar : UIView = statusBarWindow.value(forKey: "statusBar") as! UIView
if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = color
}
}