With iOS13 I cannot change the background color of the statusbar anymore because the statusbar is no longer accessible using "value for key". Did anybody figure out how this is possible or is there any knowledge that it will be possible in the final version of iOS13?
I came across different proposals already like using the UIApplications StatusBarView (no longer accessible in xcode 11, beta 7) or using the statusbarmanager. Both do not give access to the status bar.
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to: #selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = <Some Color>
}
I expect the status bar to get the background color that I need.
'statusBarFrame' was deprecated in iOS 13.0: Use the statusBarManager property of the window scene instead. I think the simplest way is to use NavigationController instead of ViewController. Changing of Navigation Bar background using storyboard will also reflect on status bar
With the arrival of iOS 13 statusBar's view is no longer accessible trough: value (forKey: "statusBar") as? UIView Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window.
Build and Run the project, The content of the status bar is dark again, which is the default. The reason for this is, iOS asked for the style of the status bar of the navigation controller instead of the contained view controller.
Select the View and in the Attributes Inspector change the Background Color to Light Gray. Build and Run the Project. The default style of the status bar is dark content. The style of the status bar can be changed to a status bar with white content.
Objective-C Version using UIStatusBarManager:
UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
statusBar.backgroundColor = [UIColor whiteColor];
[[UIApplication sharedApplication].keyWindow addSubview:statusBar]
Use this on your preferred ViewController
override func viewDidAppear(_ animated: Bool) {
if #available(iOS 13, *)
{
let statusBar = UIView(frame: (UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame)!)
statusBar.backgroundColor = UIColor.systemBackground
UIApplication.shared.keyWindow?.addSubview(statusBar)
}
}
you can use your Color in "UIColor.systemBackground"
Code works for Swift 5+ and iOS 13+
To change the StatusBar background colour, i have created a base class of UIViewController so that i can inherit same code in all the view controllers.
Added "statusBarColorChange()" to UIViewController extension.
extension UIViewController {
func statusBarColorChange() {
if #available(iOS 13.0, *) {
let statusBar = UIView(frame: UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
statusBar.backgroundColor = .appNavigationThemeColor
statusBar.tag = 100
UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.addSubview(statusBar)
} else {
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = .appNavigationThemeColor
}
} }
Created Base Class and inherited in other classes.
class BaseClass: UIViewController {
override func viewWillAppear(_ animated: Bool) {
self.statusBarColorChange()
}
}
class ChatListViewController: BaseClass {}
Hope will be helping :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With