Home » Ios » iOS 7 and later: set status bar style per view controller

iOS 7 and later: set status bar style per view controller

Posted by: admin November 30, 2017 Leave a comment

Questions:

I tried many ways to set the status bar style (default or lightcontent) but can’t get it to work on a per view controller basis. I can set the status bar style for the whole app only.

Does anyone have a hint?

I tried UIViewControllerBasedStatusBarAppearance

and

-(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
}

but these methods don’t work.

Answers:

Have you tried this?

  1. Set “View controller-based status bar appearance” (UIViewControllerBasedStatusBarAppearance) to YES in your Info.plist. (YES is the default, so you can also just leave this value out of your plist.)

  2. In your viewDidLoad method, call [self setNeedsStatusBarAppearanceUpdate].

  3. Implement preferredStatusBarStyle, returning the status bar style that you want for this view controller.

    - (UIStatusBarStyle) preferredStatusBarStyle { 
        return UIStatusBarStyleLightContent; 
    }
    
Questions:
Answers:

There is a catch here if your view controller is inside standalone UINavigationController and not a part of Storyboard based UINavigationController then above all methods fail. I came across this situation and then in order to set the status bar to light style i used following

[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];

This worked perfectly for me.

Questions:
Answers:

EDIT: This solution is deprecated on iOS 9. Please choose one of the other answers.

With UIViewControllerBasedStatusBarAppearance set to NO, I was able to set the style to white text by using:

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackTranslucent;

This is because the text color on this style was white on iOS 6 and below.

UPDATE: According to @jowie you can try that on iOS8:

[UIApplication sharedApplication].statusBarStyle = UIBarStyleBlack;

Questions:
Answers:

On viewDidLoad method, put this:

Objective C

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[self setNeedsStatusBarAppearanceUpdate];

Swift

UIApplication.shared.statusBarStyle = .lightContent
self.setNeedsStatusBarAppearanceUpdate()

Questions:
Answers:

In Swift I was able to do this by writing:

let tbc : UITabBarController = self.window?.rootViewController as UITabBarController
var moreViewController : UINavigationController = tbc.moreNavigationController

moreViewController.navigationBar.barStyle = UIBarStyle.Black

Basically you’re interested in the last line.
This resulted in tabbar changing to white:

enter image description here

Note that I didn’t change anything in Info.plist in order to achieve this result.
For more informations regarding changing Navigation Status Bar, please check out this link:
http://www.appcoda.com/customize-navigation-status-bar-ios-7/

Questions:
Answers:

I bet you have your view controller embedded into a navigation controller. To avoid setting the navigation bar’s style to .Black use this subclass:

class YourNavigationController: UINavigationController {
    override func childViewControllerForStatusBarStyle() -> UIViewController? {
        return topViewController
    }
}

Questions:
Answers:

Swift:

let tbc : UITabBarController = self.window?.rootViewController as UITabBarController
var moreViewController : UINavigationController = tbc.moreNavigationController

moreViewController.navigationBar.barStyle = UIBarStyle.Black

Objective C:

append this to the controller.m file viewDidLoad method:

[self setNeedsStatusBarAppearanceUpdate].

then implement this method in that same controller.m file:

- (UIStatusBarStyle) preferredStatusBarStyle { 
    return UIStatusBarStyleLightContent; 
}

Official docs:

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/Bars.html

Featured Article:

http://www.ryadel.com/2015/03/04/xcode-set-status-bar-style-and-color-in-objective-c/

Questions:
Answers:

In the ViewController that you want to change the status bar’s color

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void) viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

Questions:
Answers:

Swift extension for this because I always forget how this works

extension UIViewController {
    // utility to set the status bar appearance
    // Note: Make sure "View controller-based status bar appearance" is set to NO in your target settings or this won't work
    func setStatusBarForDarkBackground(dark: Bool) {
        UIApplication.sharedApplication().statusBarStyle = dark ? .LightContent : .Default
        setNeedsStatusBarAppearanceUpdate()
    }
}