ChatGPT解决这个技术问题 Extra ChatGPT

How to change Status Bar text color in iOS

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?

This link has interesting info on this issue: doubleencore.com/2013/09/…
Look my naswer for better solution stackoverflow.com/questions/17678881/…
Xcode seems to be constantly changing this, so I recommend scrolling down to find the more recent solutions (e.g., Super simple answer as of 2021).

k
krjw

Set the UIViewControllerBasedStatusBarAppearance to YES 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())

I didn't need Step 2.
Doesn't seem to be working on iOS 7 Beta 5 with Xcode5-DP5. preferredStatusBarStyle doesn't seem to get called.
UIViewControllerBasedStatusBarAppearance is changed to View controller-based status bar appearance in Xcode 5 GM seed
Got the answer for Xcode GM Seed : 1. In Info.plist put View controller-based status bar appearance as NO 2. In appDelegate, inside appDidFinishLaunching method, put [[UIView appearance] setTintColor:[UIColor whiteColor]];
UINavigationController is a special case, the above will not work. Just spent hours scratching my head over this. See here for solution: stackoverflow.com/a/19513714/505457
s
squarefrog

Alternatively, you can opt out of the view-controller based status bar appearance:

Set View controller-based status bar appearance to NO in your Info.plist. Call [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

Note: This method has been deprecated in iOS9. Use preferredStatusBarStyle on the UIViewController instead. (see Apple Developer Library)


This is a much better and easier route.
No need to code. It can all be done from the plist: (1) set "View controller-based status bar appearance" to NO and (2) set "Status bar style" to "Opaque black style". (Raw values are UIViewControllerBasedStatusBarAppearance -> NO and UIStatusBarStyle -> UIStatusBarStyleBlackOpaque)
Note that you must set the relevant info.plist value to NO for this to work.
@gothicdev: nice catch. Would accept your answer as it is the cleanest. Light option for Bar Style could be: UIStatusBarStyleLightContent
This method has been deprecated in iOS 9.
L
Lucas

You can do this without writing any line of code! Do the following to make the status bar text color white through the whole app

On you project plist file:

Status bar style: Transparent black style (alpha of 0.5)

View controller-based status bar appearance: NO

Status bar is initially hidden: NO


I don't know if things changed, but the UIStatusBarStyleLightContent value is not recognized by XCode, and not found in any documentation... Although it seems to work.
This one also changed it on the splash screen, whereas just setting it on the navigator wouldn't... Kudos!
Definitely the easiest way to get this done and as mentioned also works on splash screen.
There isn't anything called UIStatusBarStyleLightContent in the plist info HOWEVER there is Transparent Black which will do the same trick :) plus, you need to add View controller-based status bar appearance as it's not there originally and it's all what you need to get it to work :)
For lazy people like me, copy and past away: <key>UIStatusBarStyle</key> <string>UIStatusBarStyleLightContent</string> <key>UIViewControllerBasedStatusBarAppearance</key> <false/>
P
Pranav Kasetti

Note: The 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.


Deprecated in iOS 9. :-/
M
Mark Amery

For me, nothing happened with using all the things in the other answers (and from other sources/documentation). What did help was to set the Navigation Bar Style to "Black" in the XIB. This changed the text to white without any code at all.

https://i.stack.imgur.com/NfOK6.png


In the device none of the answers above worked for me but this one. I think it tells everything about the new system that you have to set the navigation_bar to BLACK to have the status_bar text WHITE.
After struggling through lots of things, this is what ended up working. The Style was set to default. Changed this and stripped out all the other status bar hacks and it works.
If you don't want to set all your nav bars in IB you can also set its appearance proxy. [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
Doesn't work for me in iOS7 Storyboard. Looks like this might be a side-effect os something else?
I would like to confirm that, all of the above answers not have worked for me neither. Following the image, the status bar text looks white. But I repeat again, the above ANSWERS with Xcode 6.2 and iOS 8.2 do not work.
P
Peter Mortensen

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];

This solution worked for me while the ones above did not.
Awesome but this UIViewControllerBasedStatusBarAppearance converts to same : View controller-based status bar appearance Automatically :)
A
Ajinkya Patil

You dont need to do any code for this

https://i.stack.imgur.com/wmTmN.png

& 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:

https://i.stack.imgur.com/9FLkn.png

Thats it.


I liked your approach! In my case, I was trying to figure out how to make the status bar style to be light only when it is fullscreen (w/o navigation bar). Then, I realized that all other screens should fit the same light style. So a good solution is to set for whole thing. Thanks! (:
If you don't need to change this ever while your app is running. This is the best approach.
This approach definitely works but it introduces a strange issue on iPad. In case your application supports deeplinks, and when you launch application through deeplink the backlink appearing on status bar disappears.
Best and most simple one.
the best solution for me :D
I
Irfan

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];
    …
}

I love this answer for it's the first time I'm using the "application" param ))
Dot notation and it would be perfect ;)
D
Deniss Fedotovs

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 to NO in the Info.plist. In ViewDidLoad method or anywhere, where do you want to change status bar style: [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];


UIViewControllerBasedStatusBarAppearance default values seems to be YES. "Apps default to using the new view controller-based status bar management system. To opt out of this, add a value of NO for the UIViewControllerBasedStatusBarAppearance key to your Info.plist." [bgr.com/2013/09/10/ios-7-gm-change-log-release-notes/]
Ya!!After setting the UIViewControllerBasedStatusBarAppearance to NO,status bar hides in the views.
should it be applied to all viewdidload?
You can add it just in appDelegates didFinishLoading method. Or in first views viewDidLoad method if you don't want it to change.
fantastic, this one did the job. simple and great coding.
M
Mohit Padalia

In case your UIViewController is inside a UINavigationController you will have to set the BarStyle:

-[UINavigationBar setBarStyle:UIBarStyleBlack]

Original Answer is here

https://devforums.apple.com/message/844264#844264


No, this simply makes the navigation bar colour the same as the status bar text colour. This hides the problem, not fixes it!
In iOS7 the status bar will mimic the UINavigationBar if the view contains a UINavigationController, so in many cases, this is the correct answer.
P
Peter Mortensen

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.


This was the only solution that worked for me in a storyboard-less and xib-less app. Should be helpful for non-IB users too.
This was perfect for me when I needed to set the style differently in each view controller
If you want to retain "View controller-based status bar appearance" set to yes, this is the way to go. Thanks!
P
Peter Mortensen

I'm using Xcode 6 beta 5 on a Swift project, for an iOS 7 app.

Here is what I did, and it works:

info.plist:

https://i.stack.imgur.com/31G46.png


Thanks it's look like only your solution working with latest ver of IOS7
This solution shows status bar while loading the app, in the launch screen.
Solved my issues in xCode 7.1. Latest Version. Apple forgot a couple crucial pieces to make something work. Again.
P
Pav Sidhu

Go to Project -> Target, Then set Status Bar Style to Light. It makes status-bar white from the launch screen. Then set View controller-based status bar appearance equal to NO in Info.plist.


The second part where you set the actual View controller-based status bar appearance equal to NO was the piece I was missing. Thanks
perfect, should be the top correct answer in my opinion. Thanks
J
James Rochabrun

https://i.stack.imgur.com/u4vyC.png

  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
 }

It's just awesome bro :) Thanks.
doing this has been deprecated in iOS 9 should do this on the rootviewcontroller override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
P
Peter Mortensen

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.


p
pkamb

Simply In App Delegate:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

In Swift 5, Follow the below steps:

Add key UIViewControllerBasedStatusBarAppearance and set value to false in Info.plist Add key UIStatusBarStyle and set value to UIStatusBarStyleLightContent


The one and only answer that actually worked for me.
But for this we need to opt out View controller-based status bar appearance .
Deprecated in iOS9
P
Peter Mortensen

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 to NO Set Status bar style to UIStatusBarStyleLightContent

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.


i think this is the correct answer (the part of the plist)... why would I add static code on didfinishlaunchingwithoptions... If you need same color for the whole app this is the right way
J
Josh Earl

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 call setNeedsStatusBarAppearanceUpdate.

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/


Thanks. My issue was that I didn't see that the type of "View controller-based status bar appearance" was set to String. Editing the .plist file "by hand" and putting in the boolean value cleared things up.
P
Peter Mortensen

Answer updated for for Xcode GM Seed:

In Info.plist put View controller-based status bar appearance as NO In the project, set: In ViewDidLoad: [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];


That changed the tab bar, not the status bar for me.
p
pkamb

Xcode constantly seems to change this, so this is the latest.

As of 2021 - Swift 5, Xcode 12

To change the status bar to white:

Open your Info.plist. Add key UIViewControllerBasedStatusBarAppearance and set value to No (false). The human readable version of this is "View controller-based status bar appearance". Add key UIStatusBarStyle and set value to UIStatusBarStyleLightContent (i.e., "Light Content").


This worked for me and was the easiest to follow.
M
Mohit Tomar

No need do some extra , just write this code in your viewController and get status bar color white

- (UIStatusBarStyle)preferredStatusBarStyle{return UIStatusBarStyleLightContent;}

Put this under ViewController.m after implementation. Its the easiest and works great! Thank you Mohit!
F
Fawkes

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:

https://i.stack.imgur.com/lZtBI.png

2) if you have a controller inside a navigation controller You can change it in the interface builder as follows:

https://i.stack.imgur.com/5kKP5.png

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

https://i.stack.imgur.com/Dp7xF.png

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:

https://i.stack.imgur.com/no246.png


please explain your case and I'll try to help
I had to add StatusBarIsIntiallyHidden = NO to the plist as well as ViewControllerBasedApplicationStatusBarAppearnce = NO to the plist. Then this works using part one
Did you have a splashscreen which has the same "image" as the first loading view controller? And also, in the "Info" section of your target you can try setting the "Hide status bar" to false and avoid dealing with your .plist. "StatusBarIsIntiallyHidden" flag
I do not. It is using the generated default xib.
I actually found this issue when creating a fresh project in xCode 7.1 which is why I thought it was so strange
K
Krunal

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 to YES 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
}

https://i.stack.imgur.com/8sH5i.png

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:

https://i.stack.imgur.com/LKppl.png


C
Cagdas Altinkaya

Simply calling

[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];

in the

-(BOOL)application:(UIApplication *)application 
           didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
}

method of my AppDelegate works great for me in iOS7.


Wow, lotsa crap here but this answer worked for me. I'm using Xcode 6 and iOS 8 SDK
Excellent, works for me in iOS 9 with View controller-based status bar appearance = NO
w
wrtsprt

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's Info.plist file and give it the value YES.

I'd definitely recommend having a look through the document, which, again, you can access with your Apple developer ID.


I have already tried [application setStatusBarStyle:UIStatusBarStyleLightContent]; but actually nothing changes, text color is still black
I'd love to be of more help, but like I say, I'm not familiar enough with the iOS development process to know what to suggest. Sorry!
1) set the UIViewControllerBasedStatusBarAppearance to YES in the plist 2) in viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate]; 3) add the following method:-(UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent; }
s
superm0

In my case for Swift 5, I added these lines:

override func viewDidAppear(_ animated: Bool) {
    navigationController?.navigationBar.barStyle = .black
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

preferredStatusBarStyle didn't do anything though. .black can go in viewDidLoad just fine.
C
Community

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"


p
parvind

in info.plist set the field value NO View controller-based status bar appearance and set statusbar style light in target > general setting.


winner winner chicken dinner
f
fraxool

If you want the same result with Swift, you can use this code in your AppDelegate.swift file :

UINavigationBar.appearance().barStyle = .BlackTranslucent

And the text of your status bar will be white :-) !


b
budiDino

Just to summarize, edit your project Info.plist and add:

View controller-based status bar appearance : NO

Status bar style : Opaque black style

or if you have raw key/value plist

UIViewControllerBasedStatusBarAppearance : NO

UIStatusBarStyle : Opaque black style


this worked for me and its less of a hassle than using preferredStatusBarStyle since you can configure it globally (of course that's only useful it appropriate to your case)