UIApplication.sharedApplication().setStatusBarStyle() deprecated in iOS 9
Solution 1
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.
Then I went to my target's General settings -> Deployment info
and changed the dropdown option
Status Bar Style
to Light
instead of Default
This changed the statusbar style to Light
for my whole application, just what I wanted.
I Hope this helps!
Solution 2
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:
Solution 3
In Swift 3 is like that:
UIApplication.shared.statusBarStyle = .lightContent
Solution 4
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()
Solution 5
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.
Related videos on Youtube
Rick
Updated on July 09, 2022Comments
-
Rick almost 2 years
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
-
Oleksandr Karaberov over 8 yearsHave the same problem. For all my view controllers in the application I use
BaseViewController
superclass, where I set this style. -
Rick over 8 years@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.
-
Ashish Kakkad over 8 yearsIf you are using the
UINavigationController
then here is the answer stackoverflow.com/questions/32730211/… -
Kiran Sarvaiya over 6 yearsPossible duplicate of How to set Status Bar Style in Swift 3
-
-
justdan0227 over 8 yearsIf you use this in the AppDelegate it still says its been depricated.
-
justdan0227 over 8 yearsSo 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 }
-
jeet.chanchawat over 8 yearsit is also depricated.
-
Scooter over 8 yearsThis works, but it is deprecated in iOS 9.0. Still fires a warning and will quit working in some future iOS iteration.
-
Sonic Master about 8 yearsStatus Bar Style's dropdown is missing in xcode 7.2. Where I can find it? @Rick?
-
Sonic Master about 8 years@Rick I think it was right. See my screenshot here dropbox.com/s/m40nwswf6wy8uh5/…
-
Rick about 8 years@SonicMaster Let me have a look and I will let you know
-
Sonic Master about 8 yearsAh, 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
-
Rick about 8 years@SonicMaster Ah now I get it, no problem, thanks to you aswel!
-
Despotovic over 7 yearsAny comment on why downwoting this answer? It works for me.
-
Chan Jing Hong almost 7 yearsAs 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.
-
Mostafa Berg over 6 yearsThis is the same thing
-
user1329261 over 5 yearsoc version not woking at all
-
Alessandro Francucci almost 4 yearswhat 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 almost 4 yearsIt's the same thing, deprecated
-
Alessandro Francucci almost 4 yearsIt doesn't seem a very clean solution though
-
ekashking over 3 yearsThe goal is to dynamically change the status bar style ... your answer is just a basic static approach that everyone knows about.