1087

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?

2

61 Answers 61

9

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];
2
  • Navigation bar != Status bar. Mar 11, 2015 at 19:14
  • ... Did you try it!? (And yes, for the status bar)
    – eirik
    Mar 13, 2015 at 0:09
8

iOS 13 Solution(s)

UINavigationController is a subclass of UIViewController (who knew 🙃)!

Therefore, when presenting view controllers embedded in navigation controllers, you're not really presenting the embedded view controllers; you're presenting the navigation controllers! UINavigationController, as a subclass of UIViewController, inherits preferredStatusBarStyle and childForStatusBarStyle, which you can set as desired.

Any of the following methods should work:

  1. Opt out of Dark Mode entirely
    • In your info.plist, add the following property:
      • Key - UIUserInterfaceStyle (aka. "User Interface Style")
      • Value - Light
  2. Override preferredStatusBarStyle within UINavigationController

    • preferredStatusBarStyle (doc) - The preferred status bar style for the view controller
    • Subclass or extend UINavigationController

      class MyNavigationController: UINavigationController {
          override var preferredStatusBarStyle: UIStatusBarStyle {
              .lightContent
          }
      }
      

      OR

      extension UINavigationController {
          open override var preferredStatusBarStyle: UIStatusBarStyle {
              .lightContent
          }
      }
      
  3. Override childForStatusBarStyle within UINavigationController

    • childForStatusBarStyle (doc) - Called when the system needs the view controller to use for determining status bar style
    • According to Apple's documentation,

      "If your container view controller derives its status bar style from one of its child view controllers, [override this property] and return that child view controller. If you return nil or do not override this method, the status bar style for self is used. If the return value from this method changes, call the setNeedsStatusBarAppearanceUpdate() method."

    • In other words, if you don't implement solution 3 here, the system will fall back to solution 2 above.
    • Subclass or extend UINavigationController

      class MyNavigationController: UINavigationController {
          override var childForStatusBarStyle: UIViewController? {
              topViewController
          }
      }
      

      OR

      extension UINavigationController {    
          open override var childForStatusBarStyle: UIViewController? {
              topViewController
          }
      }
      
    • You can return any view controller you'd like above. I recommend one of the following:

      • topViewController (of UINavigationController) (doc) - The view controller at the top of the navigation stack
      • visibleViewController (of UINavigationController) (doc) - The view controller associated with the currently visible view in the navigation interface (hint: this can include "a view controller that was presented modally on top of the navigation controller itself")

Note: If you decide to subclass UINavigationController, remember to apply that class to your nav controllers through the identity inspector in IB.

P.S. This works on iOS 13 😎

7

In Plist, add this:

  • Status bar style: UIStatusBarStyleLightContent
  • View controller-based status bar appearance: NO
7

Let me give you a complete answer to your question. Changing the status bar text color is very easy but its a little confusing in iOS 7 specially for newbies.

If you are trying to change the color from black to white in Storyboard by selecting the view controller and going to Simulated Metrics on the right side, it won't work and i don't know why. It should work by changing like this but any how.

Secondly, you won't find UIViewControllerBasedStatusBarAppearance property in your plist but by default its not there. You have to add it by yourself by clicking on the + button and then set it to NO.

ios 7 status bar text color

Lastly, you have to go to your AppDelegate.m file and add the following in didFinishLaunchingWithOptions method, add the following line:

     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

This will change the color to white for all your view controllers. Hope this helps!

1
  • This method is deprecated on iOS 9
    – Trung Bui
    Feb 5, 2016 at 16:26
6

The key to making this work is that only the fullscreen view controller get's to dictate the style of the status bar.

If you are using a navigation controller and want to control the status bar on a per view controller basis, you'll want to subclass UINavigationController and implement preferredStatusBarStyle such that it returns the topViewController's preference.

Make sure you change the class reference in your storyboard scene fromUINavigationController to your subclass (e.g. MyNavigationController in the example below).

(The following works for me. If your app is TabBar based, you'll want to do something similar by subclassing the UITabBarController but I haven't tried that out).

@interface MyNavigationController : UINavigationController

@end

@implementation MyNavigationController

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return self.topViewController.preferredStatusBarStyle;
}

@end
6

Swift 3 - Xcode 8.

If you want status bar to initially hidden on Launch screen then try this,

Step 1: Add following to info.plist.

  • View controller-based status bar appearance value NO
  • Status bar is initially hidden value YES

Step 2: Write this in didFinishLaunchingWithOptions method.

UIApplication.shared.isStatusBarHidden = false
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
5

You can do this from info.plist:

1) "View controller-based status bar appearance" set to "NO"

2) "Status bar style" set to "UIStatusBarStyleLightContent"

done

5

For Xcode 5.1:

Add "View controller-based status bar appearance" to NO in the .plist.

In AppDelegate, add:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

That's all!

4

I make this in iOS 9 and Swift 2.0 if I use UINavigationController

self.navigationController?.navigationBar.barStyle = UIBarStyle.Black

And I make this if I use modal segue

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

In Info.plist set 'View controller-based status bar appearance' as NO

In AppDelegate add

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
4

In iOS 8: add NavigationController.NavigationBar.BarStyle = UIBarStyle.Black; to viewDidLoad

4

change the status bar text color for all ViewControllers

swift 3

if View controller-based status bar appearance = YES in Info.plist

then use this extension for all NavigationController

extension UINavigationController
{
    override open var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
     }
 }

if there is no UINavigationController and only have UIViewController then use Below code:

extension UIViewController
{
    override open var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
     }
 }

objective c

create category class

For UIViewController

In UIViewController+StatusBarStyle.h

 @interface UIViewController (StatusBarStyle)
 @end

In UIViewController+StatusBarStyle.m

 #import "UIViewController+StatusBarStyle.h"

 @implementation UIViewController (StatusBarStyle)
 -(UIStatusBarStyle)preferredStatusBarStyle
 {
  return UIStatusBarStyleLightContent;
 }
 @end 

For UINavigationController

In UINavigationController+StatusBarStyle.h

 @interface UINavigationController (StatusBarStyle)
 @end

In UINavigationController+StatusBarStyle.m

 #import "UINavigationController+StatusBarStyle.h"

 @implementation UINavigationController (StatusBarStyle)
 -(UIStatusBarStyle)preferredStatusBarStyle
 {
  return UIStatusBarStyleLightContent;
 }
 @end  
3

If your application needs to have UIStatusBarStyleLightContent by default, but you still want to have the ability to use UIStatusBarStyleDefault on some screens, you could choose to manage the status bar color on the controller level, but in this case you'll have to overwrite preferredStatusBarStyle in every view controller (or implement it in a base view controller, from which all your other view controllers will inherit). Here's another way of solving this problem:

  • Set the UIViewControllerBasedStatusBarAppearance to NO in the plist
  • Set the UIStatusBarStyle to UIStatusBarStyleLightContent

All view controllers will use white text for the status bar. Now add this methods only in the view controllers that need the status bar with black text:

-(void)viewWillAppear:(BOOL)animated  
{  
  [super viewWillAppear:animated];  
  [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}    

-(void)viewWillDisappear:(BOOL)animated  
{  
  [super viewWillAppear:animated];  
  [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}    
3

The easiest way to do this from Xcode (without any coding) is:

  • Add View controller-based status bar appearance to your Info.plist and set the value to NO.
  • Now, go to your project target and inside Deployment Info you'll find an option for Status Bar Style. Set the value of this option to Light.

You'll have the White status bar.

3

In my case nothing helped. I was trying to change StatusBar color at the ViewController2, that was embded in the NavigationController, which, in turn, was presented modally from ViewController1. This way not worked:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .darkContent
}

Nothing happened, until I found this solution: Add to ViewController1 this line -

navigationController.modalPresentationCapturesStatusBarAppearance = true

let navigationController = UINavigationController(rootViewController: viewController2)
navigationController.modalPresentationStyle = .overFullScreen
navigationController.modalTransitionStyle = .crossDissolve           
navigationController.modalPresentationCapturesStatusBarAppearance = true
self.present(navigationController, animated: true)

So if you have navigation scheme similar to ViewController1 presented ViewController2, try modalPresentationCapturesStatusBarAppearance property of the presented one

Documentation:

The default value of this property is false.

When you present a view controller by calling the present(_:animated:completion:) method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller's modalPresentationStyle value is UIModalPresentationStyle.fullScreen. By setting this property to true, you specify the presented view controller controls status bar appearance, even though presented non-fullscreen.

The system ignores this property’s value for a view controller presented fullscreen.

2

This does seem to be an issue with the current build of Xcode and iOS 7.

Some related content on Apple's Developer Forums is in a search for UIStatusBarStyleLightContent in "iOS 7 Beta Livability" on the Apple Developer Forums* (currently 32 posts).

I came across it trying to set it to the light version.

(This is just a follow up on Aaron's answer.)

2
  • Delete the View controller-based status bar appearance in .plist file (if u have create) and recreate it.

  • set Status Bar style to Opaque black style

In appDelegate add the following code under didFinishLaunching.

 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
0
2

This worked for me:

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the plist

  2. The rootViewController needs the method implementation for

    -(UIStatusBarStyle)preferredStatusBarStyle
    

Because my rootViewController is managed by Cocoapods (JASidePanelController) I added this method through a category:

#import "JASidePanelController+StatusBarStyle.h"

@implementation JASidePanelController (StatusBarStyle)

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

@end
2

Just Change in 1) Info.plist View controller-based status bar appearance -> NO and write 2)

  [[UIApplication
 sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent]; 

in

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
2

Update for iOS 17+

Considering the App under it and modifies status bar style automatically by Operating system itself.

2

This answer is with the help of hackingwithswift website

for iOS (13, *)

Some Time we need different colour of status bar, for example for one ViewController we need the black status bar, and for the second ViewController we need the white status bar. Now what we have to do? We need to add this peace of code in ViewController

    // MARK: - Variables
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    // MARK: - View Life Cycle
    override func viewDidAppear(_ animated: Bool) {
        setNeedsStatusBarAppearanceUpdate()
    }

This code will change the light or White colour of status bar in that particular ViewController. We can change it to .dark in preferredStatusBarStyle

For more details visits hackingwithswift

Edited

Update for Xcode 15.2

Need to add below line in viewDidAppear before setNeedsStatusBarAppearanceUpdate()

    navigationController?.navigationBar.barStyle = .black

So the Updated code will be for viewDidAppear

        // MARK: - View Life Cycle
        override func viewDidAppear(_ animated: Bool) {
            navigationController?.navigationBar.barStyle = .black
            setNeedsStatusBarAppearanceUpdate()
        }
1
1

On iOS 7, if you want to use UIViewControllerBasedStatusBarAppearance == YES, and your root view controller is UINavigationController, you should subclass it and overload childViewControllerForStatusBarStyle, for example, like this:

- (UIViewController*) childViewControllerForStatusBarStyle
{
    return self.viewControllers.lastObject;
}

After this, preferredStatusBarStyle will be called on pushed view controllers.

1

You can use this for iOS 6 and 7:

#ifdef __IPHONE_7_0
# define STATUS_STYLE UIStatusBarStyleLightContent
#else
# define STATUS_STYLE UIStatusBarStyleBlackTranslucent
#endif

[[UIApplication sharedApplication] setStatusBarStyle:STATUS_STYLE animated:YES];
1

What I had to do for swift and navigation controller

extension UINavigationController {
    override open var preferredStatusBarStyle: UIStatusBarStyle {
       return .lightContent
    }   
}
1

Please try this

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [application setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = [UIColor blackColor];
    }
1

This solution works for apps using the new SwiftUI Lifecycle / iOS 14.0:

I needed to change the status bar text color dynamically and couldn't access window.rootViewController because SceneDelegate doesn't exist for the SwiftUI Lifecycle.

I finally found this easy solution by Xavier Donnellon: https://github.com/xavierdonnellon/swiftui-statusbarstyle

Copy the StatusBarController.swift file into your project and wrap your main view into a RootView:

@main
struct ProjectApp: App {     
    var body: some Scene {
        WindowGroup {
            //wrap main view in RootView
            RootView {
                //Put the view you want your app to present here
                ContentView()
                    //add necessary environment objects here 
            }
        }
    }
}

Then you can change the status bar text color by using the .statusBarStyle(.darkContent) or .statusBarStyle(.lightContent) view modifiers, or by calling e.g. UIApplication.setStatusBarStyle(.lightContent) directly.

Don't forget to set "View controller-based status bar appearance" to "YES" in Info.plist.

0

If you want to set it to any color, use below code.

id statusBarWindow = [[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
id statusBar = [statusBarWindow valueForKey:@"statusBar"];

SEL setForegroundColor_sel = NSSelectorFromString(@"setForegroundColor:");
if([statusBar respondsToSelector:setForegroundColor_sel]) {
    // iOS 7+
    [statusBar performSelector:setForegroundColor_sel withObject:YourColorHere];
                                                                 ^^^^^^^^^^^^^
}

I know I am accessing private API, but I have used this in many projects and Apple have approved it.

Just while submitting the app, send this code to Apple in Comments section and inform that you are using this code to change the status bar color.

And yes, don't forget below too.

1
  • And how can I do that in Swift? Dec 4, 2020 at 14:46
0

Here is a better solution extend Navigation controller and put in storyboard

class NVC: UINavigationController {

    override var preferredStatusBarStyle: UIStatusBarStyle {
              return .lightContent
    }

    override func viewDidLoad() {
        super.viewDidLoad()

    self.navigationBar.isHidden = true
    self.navigationController?.navigationBar.isTranslucent = false
      
     self.navigationBar.barTintColor = UIColor.white
     setStatusBarColor(view : self.view)
    }
    

    func setStatusBarColor(view : UIView){
             if #available(iOS 13.0, *) {
                 let app = UIApplication.shared
                 let statusBarHeight: CGFloat = app.statusBarFrame.size.height
                 
                 let statusbarView = UIView()
              statusbarView.backgroundColor = UIColor.black
                 view.addSubview(statusbarView)
               
                 statusbarView.translatesAutoresizingMaskIntoConstraints = false
                 statusbarView.heightAnchor
                     .constraint(equalToConstant: statusBarHeight).isActive = true
                 statusbarView.widthAnchor
                     .constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
                 statusbarView.topAnchor
                     .constraint(equalTo: view.topAnchor).isActive = true
                 statusbarView.centerXAnchor
                     .constraint(equalTo: view.centerXAnchor).isActive = true
               
             } else {
                 let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
              statusBar?.backgroundColor = UIColor.black
             }
         }
}

status bar color will be black and text will be white

1
  • great work, I respect your solution, but I will not recommend this solution to anyone, I saw many times developer add a new class with inheritance, and use that rapper class in code (mostly write for labels and navigation controller). In my suggestion we should not write wrapper class to achieve this functionality, we always use the extensions for it or use the native behaviour to solve this kind of problem. I hope so you did not mind on my opinion. Thanks. Jun 29, 2022 at 5:37
0

Its working fine and simple code for each view controller

override var preferredStatusBarStyle: UIStatusBarStyle {
        return .white
    }

Below, we are using different styles for each ViewController. You can set the status bar color with a simple override method.

enter image description here

Don't forget to set the View controller-based status bar appearance to YES in your Info.plist.

-2
extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }
}
1
  • Can you provide some explanation for why this works?
    – economy
    Mar 17, 2020 at 19:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.