iOS and the status bar

Since iOS 7, there is this UIViewControllerBasedStatusBarAppearance or “View controller-based status bar appearance” option in Info.plist that aims to manage the way status bar appearance is set across the app.

Typical answers on Stack Overflow to questions regarding status bar text color not changing to white when using the UIApplication setStatusBarStyle: method or not disappearing when hiding will say:

Just set the “View controller-based status bar appearance” option to NO in your Info.plist.

That’s a mistake. By doing this, you are turning off the feature introduced in iOS 7. Even if I agree that this is convenient, I could not encourage you more to work with UIViewControllerBasedStatusBarAppearance set to YES.

Here are some things I learned trying to get it to work:

  1. The “Status bar style” and “Status bar is initially hidden” are still used during launch screen.
  2. If a view controller is not contained in a navigation controller and if you want to have a status bar with text colored in white, then you have to override the preferredStatusBarStyle method to say that you want the light content status bar style
  3. If you are using navigation controllers, the previous method is ignored so you should use [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];, status bar style is automatically set light content in this case
  4. To hide status bar, there is also this “prefersStatusBarHidden” method that you can override
  5. The overridden methods are called when  your view controller is first displayed, but you can force iOS to update status bar settings by calling [self setNeedsStatusBarAppearanceUpdate]; at runtime

A good example of something you can’t achieve with UIViewControllerBasedStatusBarAppearance set to NO: is getting the status bar to white text color when opening a MFMailComposeViewController; even with status bar default color globally set, the status bar stays written in black. To bypass that, you must change the option value to YES, and you can, for instance, create a category like this one:

[cc lang=”objc” lines=”-1″]
#import “MFMailComposeViewController+StatusBarStyle.h”
@implementation MFMailComposeViewController (StatusBarStyle)
#pragma mark – Status bar management
– (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
– (UIViewController *)childViewControllerForStatusBarStyle
{
return nil;
}
@end
[/cc]

Part of the credit goes to this post: https://coderwall.com/p/hitv1q/mastering-ios-7-uistatusbarstyle


Next up


Leave a Reply

Your email address will not be published. Required fields are marked *