65

My App’s background colour is black. Cause the whole view is below the status bar on iOS 7, the content on the status bar will hard to be distinguished. So how to change status bar’s content colour to white?

I've tried preferredStatusBarStyle and several other ways, but failed.

Kjuly
  • 34,476
  • 22
  • 104
  • 118

10 Answers10

187
  1. Set "View controller-based status bar appearance” to NO in your info.list file;
  2. Insert

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    

    to -application:didFinishLaunchingWithOptions: of the AppDelegate.m.


Note: UIStatusBarStyleDefault is the default value for the status bar style, it'll show black content instead. Both UIStatusBarStyleBlackTranslucent & UIStatusBarStyleBlackOpaque are deprecated in iOS 7.0.


UPDATE for iOS 9:

As @ZakariaDarwish mentioned, the method -setStatusBarStyle is deprecated in iOS 9. (Note: The original question was asked for iOS 7 long time ago, and I don't support it now, the new solution below works for me under iOS 9, hence update here.)

So, the only way left (at least for now) is to implement -preferredStatusBarStyle in your view controller (remember to set "View controller-based status bar appearance" back to YES).

You can invoke UIViewController's instance method -setNeedsStatusBarAppearanceUpdate once value changed in -preferredStatusBarStyle or -prefersStatusBarHidden.

There're also two methods -childViewControllerForStatusBarStyle & -childViewControllerForStatusBarHidden to return the preferred style from child view controller as you want.

e.g., if you used below methods

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];

to switch status bar style before, you can use code sample below

- (void)shouldChangeStatusBarStyleToLightContent:(BOOL)toLightContent
                                        animated:(BOOL)animated
{
  _shouldChangeStatusBarStyleToLightContent = toLightContent;
  if (animated) {
    [UIView animateWithDuration:.3f animations:^{ [self setNeedsStatusBarAppearanceUpdate]; }];
  } else {
    [self setNeedsStatusBarAppearanceUpdate];
  }
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
  return (_shouldChangeStatusBarStyleToLightContent ? UIStatusBarStyleLightContent : UIStatusBarStyleDefault);
}

for this updated solution now.

Community
  • 1
  • 1
Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • 5
    Only a tip: You can use application instead of [UIApplication sharedApplication] if you are setting this in -application:didFinishLaunchingWithOptions: – jomafer Jul 18 '14 at 10:26
  • Works on iOS 8.2, too. – uerceg Mar 18 '15 at 12:24
  • 2
    Note: because `application:didFinishLaunchingWithOptions` is not called until the application loads, this will *not* change the color of the status bar on the launch screen. – SimplGy Jun 13 '15 at 16:02
  • To also impact the launch screen, use the .plist method instead: http://stackoverflow.com/a/18898750/111243 – SimplGy Jun 13 '15 at 16:10
70

In your *-Info.plist file:

  1. Set 'View controller-based status bar appearance' to NO
  2. Set 'Status bar style' to UIStatusBarStyleLightContent

Alternatively you can specify Status bar style as 'Black Opaque' or 'Black Translucent' in General tab of the Target.(in Xcode 5.0.1) But they are obsoleted values.

Satachito
  • 5,838
  • 36
  • 43
  • 2
    Outstanding, and greatly appreciated. With the deprecation of setStatusBarStyle in iOS 9.0, this is an absolute godsend. – Terrance Shaw Jan 17 '16 at 02:29
26

I use this in main controller:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
Denis Kozhukhov
  • 1,205
  • 8
  • 16
14

Place these two keys in info.plist

enter image description here

NaXir
  • 2,373
  • 2
  • 24
  • 31
9

Here a short and simple solution to set status bar color White

1) First copy this line View controller-based status bar appearance to in your .plist file and set Boolean NO;

2) In your AppDelegate.m file under didFinishLaunchingWithOptions paste this

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:NO];

OR add in .plist

enter image description here

Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
6

iOS 9 (deprecated warning workaround)

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
5
    #ifdef __IPHONE_7_0
    # define STATUS_STYLE UIStatusBarStyleLightContent
    #else
    # define STATUS_STYLE UIStatusBarStyleBlackTranslucent
    #endif

    [[UIApplication sharedApplication] setStatusBarStyle:STATUS_STYLE animated:YES];
Arash Zeinoddini
  • 801
  • 13
  • 19
5

If your application have different status bar's content color for each view controller the preferred method would be implementing

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

If you need to change the bar's content color globally throughout the application then add following lines of code in your didFinishLaunchingWithOptions method in AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarStyle = .lightContent
        return true
    }

Wait setting the statusBarStyle does nothing if your application is using the default UIViewController-based status bar system. For this

Set "View controller-based status bar appearance” to NO in your info.list file

Rajan Twanabashu
  • 4,586
  • 5
  • 43
  • 55
4

Just a note, since this was there. If you are using a UINavigationController, you can throw this into the view controllers viewDidLoad method:

self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
Shawn
  • 667
  • 8
  • 19
  • 2
    This is wrong! `barStyle` is of type `UIBarStyle` which does not have `UIStatusBarStyleLightContent`. – Zorayr Jun 03 '15 at 06:55
  • Yeah it seems to work regardless. I understand it does not have the property in the class description but it seems to convert the UIBarStyle to UIStatusBarStyle for whatever reason. You can test it yourself in one line of code... – Shawn Jun 03 '15 at 14:12
  • 1
    @Shawn these enums are both of NSInteger type, this is why it doesn't yell an error. If you'll enable all warnings in build settings ,this will become a warning due to type mismatch. – art-divin Jul 05 '15 at 10:00
  • @art-divin do you know why even with the warning it follows through with my intended use? – Shawn Jul 15 '15 at 19:37
  • @shawn it works because the status bar inherits a bunch of settings from the UINavigationBar and adjusts accordingly. So color, transparency, text and content in the status bar will be adjusted based on the UIBarStyle. I found a really good [write up here](https://possiblemobile.com/2013/09/developers-guide-to-the-ios-7-status-bar/). – jriskin Aug 03 '15 at 18:11
2

To do it programmatically in Swift 3 try this anywhere in your view controller.

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
  }

I also set the plist key "View controller-based status bar appearance" to YES.

SmileBot
  • 19,393
  • 7
  • 65
  • 62