All I wanted was two things for my status bar:

I wanted it to have white text by default as it was going to be on a dark background

I wanted to be able to hide it temporarily every now and again.

Straightforward right?

Not so much… Let’s take a look at the case.

So if you hunt around, there are several ways to manipulate the status bar, let’s take a look at some:

General Settings of your Target

In the General tab for the preferences for your project’s target, you’ll find two fields that adjust the default behaviour of your status bar :

Status Bar Style — choose between Light(i.e. light text) or Default(i.e. dark text)

Hide status bar — a checkbox.

These fields are derived from the info plist, which would be an alternative approach for adjusting these fields:

Info plist fields

If you’re using info.plist to adjust the default behaviour of your status bar, you’ll want to add rows for:

Status bar is initially hidden( UIStatusBarHidden )

Choose from either YES or NO.

Status bar style(UIStatusBarStyle).

Choose from either:

default( UIStatusBarStyleDefault )

) light( UIStatusBarStyleLightContent )

Ignore that Xcode wants you to choose between Opaque black style and Transparent black style (alpha of 0.5). These are deprecated styles, it appears as though no-one told the property list editor that yet!

If you look closely, you’ll notice I have another status bar related row set up in the info.plist — another Boolean property called View controller-based status bar appearance — I’ll come back to this one in a moment.

So they are the ways to set up the defaults for the status bar style and whether it is hidden or not.

But how about if you want to temporarily change these defaults?

If you look around the internets, you’ll see two alternative solutions mentioned:

setting properties on the UIAppDelegate

overriding properties in your view controller class

Set properties on the UIAppDelegate

Some will suggest you to set properties on the UIAppDelegate :

UIApplication.shared.statusBarStyle = .default //Set Style UIApplication.shared.isStatusBarHidden = false //Set if hidden

These could be set for example in the didFinishLaunchingWithOptions method of your app’s UIAppDelegate , or anywhere you like really — for example, if you wanted to change this temporarily for a view controller you could call this in the viewDidAppear and viewDidDisappear methods.

Sounds great, right? Just call a method to update the status bar, easy.

Oh wait, sorry. I forgot to mention — the isStatusBarHidden and statusBarStyle properties were deprecated in iOS 9. Bah!

Let’s take a look at the view controller properties that Xcode is suggesting:

Override properties in your view controller classes

Within your view controller classes, you can override the prefersStatusBarHidden and preferredStatusBarStyle properties:

override var prefersStatusBarHidden: Bool {

return true

}

override var preferredStatusBarStyle: UIStatusBarStyle {

return .lightContent

}

These methods return values by default ( prefersStatusBarHidden defaults to false , preferredStatusBarStyle defaults to .default ), so if you change either of these default values, when the user navigates to another view controller, if the new view controller doesn’t have its own implementation of these methods, it will revert to these defaults. This is a difference in behavior to setting properties on the app delegate, which changes them for the duration of your app, well, at least until you specifically request they change again.

Changing mid view controller

By the way, you can use these properties to make this change mid view controller too. Just set up a variable, let’s call it statusBarHidden , to contain the current preference for the status bar.

We could then return this variable in the view controller property prefersStatusBarHidden :

var statusBarHidden = true

override var prefersStatusBarHidden: Bool {

return statusBarHidden

}

Now if we change the statusBarHidden property, nothing will happen yet. We need to tell the system that we want the status bar to change. We can do this in a property observer:

var statusBarHidden = true {

didSet(newValue) {

setNeedsStatusBarAppearanceUpdate()

}

}

Now we should be able to change the statusBarHidden property, and the status bar will automatically update. For example, we could have a button that toggles the status bar, connected to an IBAction :

@IBAction func toggleStatusBar(_ sender: Any) {

statusBarHidden.toggle()

//If you’re pre Swift 4.2, use:

//statusBarHidden = !statusBarHidden

}

So what do we have so far?

So, let’s summarize what our options are so far for updating the status bar:

General Settings of your Target / Info plist fields

Set properties on the UIAppDelegate (deprecated)

(deprecated) Override properties in your view controller classes

Here’s the stinger in the tail. Remember that View controller-based status bar appearance property we saw in the info plist file? Which options are available to us, depends on what we choose in this field.

If we have View controller-based status bar appearanceset to NO, we can make a change to the default status bar in the general settings or info plist. We could also set properties on the UIAppDelegate , but don’t forget, those properties are deprecated.

If we have View controller-based status bar appearance set to YES, we can override properties in our view controller classes.

Back to the problem…

Remember my original problem?

“…white text by default…” — the obvious way to do this would be change defaults in General Settings / Info.plist

“…hide it temporarily…” — the obvious way to do this would be simply to override properties in a view controller class.

Only problem is, these two solutions are mutually exclusive, I can’t set View controller-based status bar appearance to both NO and YES! Setting properties on UIAppDelegate would have been a good solution to this problem, but this technique is deprecated.

Which leaves me with the only solution I can think of. Instead of setting the default style of the status bar, I would need to set the style of the status bar on each view controller. A bit of a pain! *

* If anyone can think of a reliable and safe way to change the status bar behavior project-wide while having View controller-based status bar appearance set to YES, I’m interested to hear it!

Navigation Controllers

I’m afraid we’re not done on the mysterious case of status bars — embed your view controller in a navigation controller, and if you have View controller-based status bar appearance set to YES, suddenly the navigation controller now takes over control of the style of the status bar, and your view controller’s preferredStatusBarStyle property will be ignored.

If you want to adjust the style of the status bar in a navigation controller, you’ll need to do this in the navigation bar’s style property, either in code, eg:

override func viewDidAppear(_ animated: Bool) {

navigationController?.navigationBar.barStyle = .black

}

Or in the storyboard:

To make things ultra-confusing, if you want, for example, that your status bar has white text, view controllers not embedded in a navigation controller will need to request the .lightContentstyle (i.e. light colored writing), and view controllers that are embedded in a navigation controller will need to request the .blackstyle (i.e. a black colored nav bar).

Don’t forget, if you have View controller-based status bar appearance set to NO, any changes you make to the navigation bar style will be ignored.

In summary

If View controller-based status bar appearance = NO:

General Settings of your Target / Info.plist fields

Set properties on the AppDelegate (deprecated, remember)

If View controller-based status bar appearance = YES:

Override properties in your view controller classes

Set style of navigation controllers / navigation bars.

So, mystery solved… sort of! If you ask me, it’s all a bit unnecessarily complicated, but there it is, the status of the status bar!