Warning: It does not work anymore with iOS 13 and Xcode 11.
========================================================================
I had to try look for other ways. Which does not involve addSubview
on window. Because I am moving up the window when keyboard is presented.
Objective-C
- (void)setStatusBarBackgroundColor:(UIColor *)color {
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
Swift
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
Swift 3
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}
Calling this form application:didFinishLaunchingWithOptions
worked for me.
N.B. We have an app in the app store with this logic. So I guess it is okay with the app store policy.
Edit:
Use at your own risk. Form the commenter @Sebyddd
I had one app rejected cause of this, while another was accepted just
fine. They do consider it private API usage, so you are subject to
luck during the reviewing process 🙂 – Sebyddd