I have been using
UIApplication.sharedApplication().setStatusBarStyle()
In my appDelegate and it has worked fine, but since iOS 9, this method
This worked fine for me in Xcode 7.
In AppDelegate:
UIApplication.sharedApplication().statusBarStyle = .LightContent
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()
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;
}
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
}
}
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.
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: