In XCode 7.3.x ill changed the background Color for my StatusBar with:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
But it seems that this is not working anymore with Swift 3.0.
Ill tried with:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = (UIApplication.shared.value(forKey: "statusBarWindow") as AnyObject).value(forKey: "statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
But it gives me:
this class is not key value coding-compliant for the key statusBar.
Any Ideas how to change it with XCode8/Swift 3.0?
For iOS 11 and Xcode 9 use the following steps.
create an extention to UIApplication class:
extension UIApplication { var statusBarView: UIView? { return value(forKey: "statusBar") as? UIView } }
In your class or wherever you want to change the Status bar's background color:
UIApplication.shared.statusBarView?.backgroundColor = .red
For light content or dark content of status bar simply go to Info.plist and add the following value row with value NO.
This works for me, as my navigation barTintColor was black and unable to see the status bar.
When set above code it didFinishLaunch status bar appears in white.
With using Swift 3 and 4 you can use the code snippet on below. It finds the view from
UIApplication
usingvalueForKeyPath
as set it's background color.Objective-C
"Change" status bar background color:
Change status bar text color:
Update: please note that the status bar frame will change when the view is rotated. You could update the created subview frame by:
statusBarView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
NSNotification.Name.UIApplicationWillChangeStatusBarOrientation
viewWillLayoutSubviews()
For Xcode 9 and iOS 11: The style of the status bar we will try to achieve is a status bar with white content. Go to the ViewController.swift file and add the following lines of code.
Or from project settings option you can change status bar's style:
Next, go back to the Storyboard, Select the View Controller and in the Editor menu Select Embed in Navigation Controller. Select the Navigation Bar and in the Attribute Inspector set the Bar Tint color to red. The Storyboard will look like this.![enter image description here](https://i.stack.imgur.com/K4Fzs.png)
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.
To change the style of all navigation controller inside the app, change the following method in the AppDelegate.swift file.
Build and Run the Project again, this time the content of the status bar changed to white.