My application has a dark background, but in iOS 7 the status bar became transparent. So I can't see anything there, only the green battery indicator in the corner. How can I change the status bar text color to white like it is on the home screen?
30 Answers
Set the
UIViewControllerBasedStatusBarAppearance
toYES
in the .plist file.In the
viewDidLoad
do a[self setNeedsStatusBarAppearanceUpdate];
Add the following method:
- (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; }
Note: This does not work for controllers inside UINavigationController
, please see Tyson's comment below :)
Swift 3 - This will work controllers inside UINavigationController
. Add this code inside your controller.
// Preferred status bar style lightContent to use on dark background.
// Swift 3
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Swift 5 and SwiftUI
For SwiftUI create a new swift file called HostingController.swift
import Foundation
import UIKit
import SwiftUI
class HostingController: UIHostingController<ContentView> {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
Then change the following lines of code in the SceneDelegate.swift
window.rootViewController = UIHostingController(rootView: ContentView())
to
window.rootViewController = HostingController(rootView: ContentView())
Alternatively, you can opt out of the view-controller based status bar appearance:
- Set
View controller-based status bar appearance
toNO
in yourInfo.plist
. - Call
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Note: This method has been deprecated in iOS9. Use preferredStatusBarStyle
on the UIViewController instead. (see Apple Developer Library)
Note: Most upvoted answer does not work for iOS 7 / 8
In Info.plist set 'View controller-based status bar appearance' as NO
In AppDelegate add
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
......
...
}
This solution works for iOS 7 / 8
None of that worked for me, so here is a working solution...
In Info.plist
, add a row:
UIViewControllerBasedStatusBarAppearance
, and set the value NO
.
Then in AppDelegate in didFinishLaunchingWithOptions
, add these rows:
[application setStatusBarHidden:NO];
[application setStatusBarStyle:UIStatusBarStyleLightContent];
You dont need to do any code for this
You need to add "View controller-based status bar appearance" key in info.plist as follows:
& set its value type to Boolean & value to NO. Then click on project settings,then click on General Tab & under Deployment Info set the preferred status bar style to .Light as follows:
Thats it.
Just two steps as following:
Step 1:
Under the Info tab of the project target
, Add Row:
UIViewControllerBasedStatusBarAppearance
, set value NO
.
Step 2:
In the project AppDelegate.m
:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
…
[application setStatusBarStyle:UIStatusBarStyleLightContent];
…
}
This works in Golden Master iOS 7 and Xcode 5 GM seed and iOS7 SDK released on September 18th, 2013 (at least with navigation controller hidden):
Set
the UIViewControllerBasedStatusBarAppearance
toNO
in theInfo.plist
.In
ViewDidLoad
method or anywhere, where do you want to change status bar style:[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
If you have an embedded navigation controller created via Interface Builder, be sure to set the following in a class that manages your navigation controller:
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
That should be all you need.
In AppDelegate.m, add the following.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
And in the Plist file, set 'View controller-based status bar appearance' to NO.
In Swift 3 is very easy just with 2 steps.
Go to your info.plist and change the key View controller-based status bar appearance
to "NO".
Then in the Appdelegate just add this line in didfinishlaunchingwithoptions method
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UIApplication.shared.statusBarStyle = .lightContent
return true
}
this has been deprecated in iOS9 now you should do override this property in the rootviewcontroller
doing this has been deprecated in iOS 9 should do this on the rootviewcontroller
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Well, this is really working like a piece of cake for me.
Go to your app's info.plist
.
- Set
View controller-based status bar appearance
toNO
- Set
Status bar style
toUIStatusBarStyleLightContent
Then go to your app's delegate and paste in the following code where you set your windows's RootViewController.
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 20)];
view.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];
[self.window.rootViewController.view addSubview:view];
}
Bingo. It's working for me.
iOS 7 allows individual view controllers to determine the appearance of the status bar, as described by the Apple developer documentation:
iOS 7 gives view controllers the ability to adjust the style of the status bar while the app is running. A good way to change the status bar style dynamically is to implement
preferredStatusBarStyle
and—within an animation block—update the status bar appearance and callsetNeedsStatusBarAppearanceUpdate
.
Setting the status bar appearance globally is a two-step process.
First, you need to tell iOS that you don't want to set the status bar appearance on a view-by-view basis.
Then you need to take charge and actually set the new global status bar style.
To disable view-by-view status bar control, you'll need to set the View controller-based status bar appearance
property in Info.plist
.
Open the Project Navigator and select the project for your iOS app, then select the Info tab.
Hover over a row, then click the plus sign that appears to add a new property to your .plist
.
Enter View controller-based status bar appearance
in the Key field, then make sure the Type field is set to Boolean
. Finally, enter NO
in the Value field.
To set a global style for the status bar, add another property under the Info tab with a key of Status bar style
, a Type of String
and a Value of Opaque black style
.
Here's a blog post with a little more detail and some sample code:
http://codebleep.com/setting-the-status-bar-text-color-in-ios-7/
I think all the answers do not really point the problem because all of them work in specific scenarios. But if you need to cover all the cases follow the points bellow:
Depending on where you need the status bar light style you should always have in mind these 3 points:
1)If you need the status bar at the launch screen or in other places, where you can't control it (not in view controllers, but rather some system controlled elements/moments like Launch Screen) You go to your project settings
2) if you have a controller inside a navigation controller You can change it in the interface builder as follows:
a) Select the navigation bar of your navigation controller
b) Then set the style of the navigation bar to "Black", because this means you'll have a "black" -> dark background under your status bar, so it will set the status bar to white
Or do it in code as follows
navigationController?.navigationBar.barStyle = UIBarStyle.Black
3) If you have the controller alone that needs to have it's own status bar style and it's not embedded in some container structure as a UINavigationController
Set the status bar style in code for the controller:
Here is Apple Guidelines/Instruction about status bar change. Only Dark & light (while & black) are allowed in status bar.
Here is - How to change status bar style:
If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance
to NO
in your `.plist' file.
if you wan to set status bar style, at view controller level then follow these steps:
- Set the
UIViewControllerBasedStatusBarAppearance
toYES
in the.plist
file, if you need to set status bar style at UIViewController level only. In the viewDidLoad add function -
setNeedsStatusBarAppearanceUpdate
override preferredStatusBarStyle in your view controller.
-
override func viewDidLoad() {
super.viewDidLoad()
self.setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Set value of .plist according to status bar style setup level.
Here is some hacky trick to change/set background color for status bar during application launch or during viewDidLoad of your view controller.
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
Here is result:
This is documented in the iOS 7 UI Transition Guide, which you need an Apple developer ID to access directly. The relevant excerpt:
Because the status bar is transparent, the view behind it shows through. [...] Use a
UIStatusBarStyle
constant to specify whether the statusbar content should be dark or light:
UIStatusBarStyleDefault
displays dark content. [...]
UIStatusBarStyleLightContent
displays light content. Use when dark content is behind the status bar.
Also possibly of interest:
In iOS 7, you can control the style of the status bar from an individual vew controller and change it while the app runs. To opt in to this behavior, add the
UIViewControllerBasedStatusBarAppearance
key to an app'sInfo.plist
file and give it the valueYES
.
I'd definitely recommend having a look through the document, which, again, you can access with your Apple developer ID.
I did some things different and it works for me.
With no changes in code, I did config my .plist file like this:
- View controller-based status bar appearance > NO
- Status bar style > UIStatusBarStyleLightContent (simple string)
I hope it helps.
edit
For each view controller I change the "status bar"'s Simulated Metrics property, in storyboard, from "inferred" to "Light Content"
If you still want to use View controller-based status bar appearance
in info.plist set to YES, meaning that you can change the statusbar for each view-controller, use the following for white text in the status-bar in ViewDidLoad:
[[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlackTranslucent];