44

I have been using

UIApplication.sharedApplication().setStatusBarStyle()

In my appDelegate and it has worked fine, but since iOS 9, this method is deprecated and I can't find an alternative.

I want to change the statusbar style to .LightContent for my whole application, but the only suggestion xCode gives me is to handle this in every VC separately with;

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent
}

Has anyone an idea how to do this for the whole application?

Thanks in advance

Max Desiatov
  • 5,087
  • 3
  • 48
  • 56
Rick
  • 3,168
  • 3
  • 17
  • 16
  • 1
    Have the same problem. For all my view controllers in the application I use `BaseViewController` superclass, where I set this style. – Oleksandr Karaberov Sep 15 '15 at 12:22
  • @AlexanderKaraberov Thanks for your comment, I was thinking about your method before, but I wasn't sure if that was the best way, but it seems like the best option for now. – Rick Sep 15 '15 at 16:16
  • If you are using the `UINavigationController` then here is the answer http://stackoverflow.com/questions/32730211/how-we-can-set-the-light-content-style-of-status-bar-in-ios-9-for-whole-applicat/ – Ashish Kakkad Sep 23 '15 at 06:04
  • Possible duplicate of [How to set Status Bar Style in Swift 3](https://stackoverflow.com/questions/38740648/how-to-set-status-bar-style-in-swift-3) – Kiran Sarvaiya Nov 24 '17 at 05:36

9 Answers9

76

I think I have found a solution. I ended up setting the

View controller-based status bar appearance boolean to NO

In my info.plist file.

Info.Plist

Then I went to my target's General settings -> Deployment info and changed the dropdown option Status Bar Style to Light instead of Default

Change Status Bar Style to Light

This changed the statusbar style to Light for my whole application, just what I wanted.

I Hope this helps!

Rick
  • 3,168
  • 3
  • 17
  • 16
  • 2
    So there are two ways to do it. Either do the above and set View controller-based status bar appearance to NO and then set the Deployment Info to light, OR set View controller-based status bar appearance to YES and override in your view controller with `override func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.LightContent } ` – justdan0227 Oct 16 '15 at 13:11
  • 1
    Status Bar Style's dropdown is missing in xcode 7.2. Where I can find it? @Rick? – Sonic Master Feb 19 '16 at 07:03
  • @Rick I think it was right. See my screenshot here https://www.dropbox.com/s/m40nwswf6wy8uh5/Screenshot%202016-02-19%2014.12.39.png?dl=0 – Sonic Master Feb 19 '16 at 07:14
  • @SonicMaster Let me have a look and I will let you know – Rick Feb 19 '16 at 07:15
  • 1
    Ah, I think I know what's wrong. That dropdown only shows if I change my Deployment Info's target into Universal. It disappear if you use specific target (ipad/iphone). Thank you so much, @Rick – Sonic Master Feb 19 '16 at 07:21
  • @SonicMaster Ah now I get it, no problem, thanks to you aswel! – Rick Feb 19 '16 at 07:25
  • what I find pretty stupid is that, if I want to have the `.lightContent` as default and change to `.default` to just few view controllers, I can't... – Alessandro Francucci Jul 09 '20 at 14:56
  • The goal is to dynamically change the status bar style ... your answer is just a basic static approach that everyone knows about. – ekashking Sep 10 '20 at 19:42
11

In swift 3.

In your view controller:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return UIStatusBarStyle.lightContent
}

If you wish when the app run your launch screen also has the status bar in lightContent then:

enter image description here

Marlon Ruiz
  • 1,774
  • 16
  • 14
9

In Swift 3 is like that:

 UIApplication.shared.statusBarStyle = .lightContent
EgzonArifi
  • 830
  • 1
  • 11
  • 23
3

To dynamically update UIStatusBarStyle on view controllers use this method

this will also remove deprecated warning

'setStatusBarStyle:' is deprecated: first deprecated in iOS 9.0 - Use -[UIViewController preferredStatusBarStyle]

for calling

[[UIApplication sharedApplication] setStatusBarStyle:style];

Let's Get Started

Objective - C

define UtilityFunction

+(void)setStatusBarStyle:(UIStatusBarStyle )style {    
    [[NSUserDefaults standardUserDefaults] setInteger:style forKey:@"UIStatusBarStyle"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

over-ride this method in your BaseViewController

- (UIStatusBarStyle)preferredStatusBarStyle {
    UIStatusBarStyle style = [[NSUserDefaults standardUserDefaults] integerForKey:@"UIStatusBarStyle"];
    return style;
}

set UIStatusBarStyle value for the AnyViewController using a UtilityFunction like below:

[UtilityFunctions setStatusBarStyle:UIStatusBarStyleDefault];

// call below code for preferred style
[self preferredStatusBarStyle];

Swift 4.0

define UtilityFunction

class func setPreferedStyle(style:UIStatusBarStyle)->Void {
    UserDefaults.standard.set(style, forKey: "UIStatusBarStyle")
    UserDefaults.standard.synchronize()
}

over-ride this method in your BaseViewController

override var preferredStatusBarStyle: UIStatusBarStyle {
    if let style: UIStatusBarStyle = UIStatusBarStyle(rawValue:UserDefaults.standard.integer(forKey: "UIStatusBarStyle")) {
        return style
    }
    return UIStatusBarStyle.lightContent
}

set UIStatusBarStyle value for the AnyViewController using a UtilityFunction like below:

Utility.setPreferedStyle(style: .lightContent)

// call below code for preferred style
preferredStatusBarStyle()
Muhammad Waqas
  • 904
  • 2
  • 10
  • 21
1

This worked fine for me in Xcode 7.

In AppDelegate:

UIApplication.sharedApplication().statusBarStyle = .LightContent
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
  • 2
    This works, but it is deprecated in iOS 9.0. Still fires a warning and will quit working in some future iOS iteration. – Scooter Feb 14 '16 at 23:54
1

Swift 5, iOS 13.5+

I'm gonna make a recap that I hope it's gonna be helpful.

#1: General solution without using preferredStatusBarStyle

To answer the question, if we don't want to care about exceptions screens and not use the preferredStatusBarStyle property from view controllers as Apple recommends, I think that indeed setting the UIViewControllerBasedStatusBarAppearance to false and changing the Status Bar Style under General settings -> Deployment info to light, as @Rick already recommended, is the way to go.

#2: Using preferredStatusBarStyle

For my case, I wanted to be able to have the UIStatusBarStyle.lightContent as default, but with some screens having the UIStatusBarStyle.default; and in these kind of cases, the solution #1 isn't possible.

Since also having a general extension to UIViewController that allows to change the default value isn't obviously possible for this property, the only and best way to proceed in these cases if we don't want to use deprecated methods, is via inheritance.

So, a possibility is to have a general BaseViewController (and also the BaseNavigationController if you use one) that you controller inherits from, that sets the preferredStatusBarStyle to .lightContent.

With this approach, now you can simply set the style to default where needed, while maintaining the lightContent as default.

Alessandro Francucci
  • 1,528
  • 17
  • 25
0

for those still working with Swift 3 in Xcode 8:

( slightly different to Marlon Ruiz's answer above, not an override function, but within viewDidLoad )

override func viewDidLoad() {
    super.viewDidLoad()

    var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}
aremvee
  • 179
  • 1
  • 13
-2

This is the new way in AppDelegate:

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
-2

In info.plist, set: View controller-based status bar appearance boolean to NO

In app delegate's didFinishLaunchingWithOptions, use function parameter application (and not the [UIApplication sharedApplication] or simillary the UIApplication.sharedApplication() in swift) to set this like so:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    application.statusBarStyle = UIStatusBarStyleLightContent;
}
Despotovic
  • 1,807
  • 2
  • 20
  • 24
  • Any comment on why downwoting this answer? It works for me. – Despotovic Sep 08 '16 at 08:21
  • 1
    As some of the users have already answered, doing it this way is not recommended because directly accessing the statusBarStyle variable is deprecated. It will show you a warning, and it might break your app in the future. – Chan Jing Hong May 20 '17 at 10:18