Somehow, no matter the query or number of answers I come across, I can't find one person who can say how to set the status bar color in iOS using Swift 3. I've seen all the suggestions where you add:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
but the thing is, I don't want a transparent status bar. I want a specific hex color for the status bar color.
Here is Apple Guidelines/Instruction about status bar change. Only Dark & light (while & black) are allowed in status bar.
Here is - How to change status bar style:
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your `.plist' file.
if you wan to set status bar style, at view controller level then follow these steps:
Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate
override preferredStatusBarStyle in your view controller.
-
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Set value of .plist according to status bar style setup level.
You can set background color for status bar during application launch or during viewDidLoad of your view controller.
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
Here is result:
There isn’t an API to set the color of the status bar’s content; if you’d like one, you should file an enhancement request here. If you want to change the color of the background behind the status bar, put a view with that background color at the top of your app’s window.
Related
I have been searching for a while now and I only found answers that describe to change color on one view controller not for all view controllers.
Is it possible to do it?
Only two steps are needed to change the status bar style for the entire app. 🙂
Step 1
Add a new property to the project's Info.plist file and set it to false.
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
Step 2
Go to your project's target and under General / Deployment Info, switch Status Bar Style from Default to Light.
Doing these steps will ensure the status bar behaves the same in the entire project.
Set the style of the status bar in AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarStyle = .lightContent
return true
}
And add the following code to your Info.plist:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
First in info.plist set View controller-based status bar appearance to NO
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = UIColor.blue
}
UIApplication.shared.statusBarStyle = .lightContent
return true
}
The output screenshot is below
You can set background color for status bar during application launch or during viewDidLoad of your view controller.
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
Here is result:
Here is Apple Guidelines/Instruction about status bar change. Only Dark & light (while & black) are allowed in status bar.
Here is - How to change status bar style:
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your `.plist' file.
Or programatically you can do it from app delegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.statusBarStyle = .lightContent
return true
}
if you wan to set status bar style, at view controller level then follow these steps:
Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate
override preferredStatusBarStyle in your view controller.
-
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Set value of .plist according to status bar style setup level.
Swift 4
In AppDelegate.swift add this extension:
extension UINavigationController {
override open var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Yes,
Step 1:
Open your info.plist and insert a new key named "View controller-based status bar appearance" to NO
Step 2:
Open the viewcontroller file where you want to change the statusBarStyle and put the following code in viewWillAppear(),
UIApplication.shared.statusBarStyle = .lightContent
Step 3 :
Also, implement the viewWillDisappear() method for that specific viewController and put the following lines of code,
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}
All the best
Important clarification
It is very important to understand two approaches to customizing the status bar.
Is it possible to change Status Bar color for all view controllers?
Boolean answer is Yes, but, in legal way, it is so close to No that answering Yes is provocative if you need colors other that black or white.
There are two approaches when it comes to customizing Status bar appearance.
First approach – one color for whole app
In info.plist you find or create a key called
View controller-based status bar appearance
and set it to NO.
What it does? It essentially establishes a setting that says that in your application, status bar appearance is not defined individually by each view controller. This is super important to understand. This means that you have uniform setting for entire app, for all screens. This is what you needed. But. You are limited to only two settings: white and black. And there's no way you can customize it using documented API.
To set this up:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.statusBarStyle = .lightContent
return true
}
Second approach – individual color for each view controller
This is the opposite. To make it work, go ahead to info.plist and set
View controller-based status bar appearance
to YES
This way, whenever a new view controller is open, status bar style is set individually if you insert this implementation in each UIViewController instance you need:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
You have the same as in first, set either dark or light style for statusbar.
Third approach – Hack!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let statusbar = value(forKey: "statusBar") as? UIView {
statusbar.backgroundColor = UIColor.blue
}
return true
}
Why hack? If you need status bar color other than black or white. Here you use undocumented API. You get statusBar object using KVC and manually set its color. This is dirty, not legal way, but so far it's the only way to set up custom color for statusbar. It may well lead your app to being rejected. But maybe you're lucky. In order to set it once and for all, you will need to set to NO the aforementioned flag so that status bar did not initialize its style with each view controller.
My status bar is black and I'm trying to set as white, so I did what I found in some questions here and put this on my AppDelegate...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Status bar color
UIApplication.shared.statusBarStyle = .lightContent
return true
}
There is no error in my console, nothing about this. The status bar text is still black. What could be causing this? There is another way to do that in swift 3.0?
ensure once in your project info.plist the row
View controller-based status bar appearance and set it to NO
In Swift3 you have to use following code, put anywhere in your view controller
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Here is Apple Guidelines/Instruction about status bar change. Only Dark & light (while & black) are allowed in status bar.
Here is - How to change status bar style:
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your `.plist' file.
if you wan to set status bar style, at view controller level then follow these steps:
Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate
override preferredStatusBarStyle in your view controller.
-
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
This question already has answers here:
Change Status Bar Background Color in Swift 3
(15 answers)
Closed 3 years ago.
I am trying to change the color of the status bar to like a blue, or some other color.
Is this possible, or does Apple not allow it?
NOTE: This solution fails under iOS 13 and later.
First in Plist set View controller-based status bar appearance to NO
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = UIColor.blue
}
UIApplication.shared.statusBarStyle = .lightContent
return true
}
The output screenshot is below
No, it's not possible with ready-made public APIs.
But with the release of iOS 7, you’re allowed to change the appearance of the status bar. Hence I am posting my workaround.
From an individual view controller by overriding the preferredStatusBarStyle:
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
Alternatively, you can set the status bar style by using the UIApplication statusBarStyle method. To do this, insert a new key named “View controller-based status bar appearance” and set the value to NO.
By disabling the “View controller-based status bar appearance”, you can set the status bar style by using the following code.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
At the end, change the UINavigationBar property tint color like below
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
You can set background color for status bar during application launch or during viewDidLoad of your view controller.
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
Here is result:
Here is Apple Guidelines/Instruction about status bar change. Only Dark & light (while & black) are allowed in status bar.
Here is - How to change status bar style:
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your `.plist' file.
if you wan to set status bar style, at view controller level then follow these steps:
Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate
override preferredStatusBarStyle in your view controller.
-
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Set value of .plist according to status bar style setup level.
Here is my workaround: create a UIView, add it to your root view of view controller as artificial status bar background
1.Create a UIView
// status bar's height is 20.0pt
CGRect frame = CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, 20.0);
UIView *fakeStatusBarBG = [[UIView alloc] initWithFrame:frame];
fakeStatusBarBG.backgroundColor = [UIColor yourColor];
2.Add it to your view controller's root view
// self is your view controller, make sure fakeStatusBarBG is the top subview in your view hierarchy
[self.view insertSubview:fakeStatusBarBG aboveSubview:yourTopSubview];
There your go.
3.(Additional)Change the content color on status bar, only white or black.
- (UIStatusBarStyle)preferredStatusBarStyle
{
if (youWantWhiteColor)
{
return UIStatusBarStyleLightContent;
}
return UIStatusBarStyleDefault;
}
This workaround does not use private API, so you're safe. :-)
I made this extension to change color of status bar
public extension UIViewController {
func setStatusBar(color: UIColor) {
let tag = 12321
if let taggedView = self.view.viewWithTag(tag){
taggedView.removeFromSuperview()
}
let overView = UIView()
overView.frame = UIApplication.shared.statusBarFrame
overView.backgroundColor = color
overView.tag = tag
self.view.addSubview(overView)
}
}
Here is usage anywhere in viewcontroller:
setStatusBar(color: .red)
I want to change status bar text color to customer color like screenshot attached.
I have used this to make it light content -
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
But text color not changing. Can anyone help?
Here is Apple Guidelines/Instruction about status bar change. Only Dark & light (while & black) are allowed in status bar. It does not allow to set a color (pink, as shown in your image) in status bar.
Here is - How to change status bar style:
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your `.plist' file.
if you wan to set status bar style, at view controller level then follow these steps:
Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate
override preferredStatusBarStyle in your view controller.
-
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Set value of .plist according to status bar style setup level.
You can set background color for status bar during application launch or during viewDidLoad of your view controller.
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
Here is result:
Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file.
In the viewDidLoad method, do a
[self setNeedsStatusBarAppearanceUpdate];
Add the following method:
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
If your application will support In your application's Info.plist, set "View controller-based status bar appearance" to NO.
In appDelegate.swift, Inside the didFinishLaunchingWithOptions function, add:
UIApplication.shared.statusBarStyle = .lightContent
Best answer is described here : How to set Status Bar Style in Swift 3
Text color of status bar could be white or black.
In my application i want to add light content status bar with translucent,transparent navigation bar. But when i make my navigation bar transparent it adjust itself with Black status bar content color. Navigation controller is compulsory in my case because table header needs to be stuck to it with plain mode. Any help from you will be appreciated.
I have used this code for making my navigation bar transparent.
self.navigationBar.translucent = true
self.navigationBar.shadowImage = UIImage()
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
I tried to make status bar appearance light by setting the bar style of navigation controller like
self.navigationController!.navigationBar.barStyle = .Black/.Default
But, still i am facing the same
if you want set for full application
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UIApplication.sharedApplication().statusBarStyle = .LightContent
return true
}
if you want to update only for view controller
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
UIApplication.sharedApplication().statusBarStyle = .LightContent
}
For iOS 9
set for full application
Just open info.plist and set UIViewControllerBasedStatusBarAppearance to false
Now update in AppDelegate's didFinishLaunchingWithOptions method
only for view controller
set View controller-based status bar appearance to YES
override below in your view controller
override func preferredStatusBarStyle() -> UIStatusBarStyle
{ return UIStatusBarStyle.LightContent }