Tom Kraina Tom Kraina - 4 years ago 241
iOS Question

iOS: Default status bar style with UIViewControllerBasedStatusBarAppearance YES

Is there a way how to set the default status bar style while keeping the

UIViewControllerBasedStatusBarAppearance
enabled?

Here is the problem, I'm dealing with:

Nearly the whole app needs to be using
UIStatusBarStyle.LightContent
as the navigation bar has a dark background. Originally,
UIViewControllerBasedStatusBarAppearance
was disabled and the following was set in in
Info.plist
for while text status status bar:

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>


This worked just fine until I found out that this
.LightContent
status bar is shown even for some of the share extensions, like Facebook Messenger, causing it to be unreadable:

Facebook Messenger Share Extension light status bar style

This could be solved by using
UIViewControllerBasedStatusBarAppearance
, but then I would need to add the following method to all of view controllers which I want to avoid as the app is quite large.

Moreover, for the one screen in the app that has light nav bar background, I was switching to dark nav bar using
UIApplication.sharedApplication().setStatusBarStyle()
but this method in deprecated in iOS 9.

Any ideas how to solve this? Swizzling?

Answer Source

Solution

The easiest and cleanest way who to achieve that is to add the following line in AppDelegate's application:willFinishLaunchingWithOptions method:

UINavigationBar.appearance().barStyle = .Black

This will make .LightContent as the default status bar style thorough the app as long as your app uses UINavigationController.

Don't forget to keep the following setting in app's Info.plist if want to use .LightContent status bar style during launch for splash screen:

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>

TL;DR

My current setup, which is very similar to many other apps, uses UITabBarController as the top most controller with UINavigationController stack for each tab.

UINavigationController takes care of the status bar style (as it should) and do not call preferredStatusBarStyle() on its child view controllers. Therefore, implementing the subclassing solution proposed by par does not work in my case.

Further subclassing the custom subclass of UINavigationController I'm using would not be a clean solution either.

Now, since the app has UIViewControllerBasedStatusBarAppearance enabled and correct status bar style everywhere in the app itself, SFSafariViewController and share extension like Messages, Mail, etc use the correct (.Default) status bar style too.

The only exception where the correct status bar style is not used is the Facebook Messenger's share extension mentioned in the question. However, it seems to be a bug in the extension itself as all apps I have tried that use .LightContent status bar style (like Twitter, for example) have the same issue - presented FB Messenger share extension from the app has a status bar with white color text.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download