How to change the status bar background color and text color on iOS 7?

238,418

Solution 1

Warning: It does not work anymore with iOS 13 and Xcode 11.

========================================================================

I had to try look for other ways. Which does not involve addSubview on window. Because I am moving up the window when keyboard is presented.

Objective-C

- (void)setStatusBarBackgroundColor:(UIColor *)color {

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

    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

Swift

func setStatusBarBackgroundColor(color: UIColor) {

    guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
        return
    }

    statusBar.backgroundColor = color
}

Swift 3

func setStatusBarBackgroundColor(color: UIColor) {

    guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }

    statusBar.backgroundColor = color
}

Calling this form application:didFinishLaunchingWithOptions worked for me.

N.B. We have an app in the app store with this logic. So I guess it is okay with the app store policy.


Edit:

Use at your own risk. Form the commenter @Sebyddd

I had one app rejected cause of this, while another was accepted just fine. They do consider it private API usage, so you are subject to luck during the reviewing process :) – Sebyddd

Solution 2

Goto your app info.plist

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

Then Goto your app delegate and paste 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,[UIScreen mainScreen].bounds.size.width, 20)];
    view.backgroundColor=[UIColor blackColor];
    [self.window.rootViewController.view addSubview:view];
}

Hope it helps.

Solution 3

While handling the background color of status bar in iOS 7, there are 2 cases

Case 1: View with Navigation Bar

In this case use the following code in your viewDidLoad method

 UIApplication *app = [UIApplication sharedApplication];
 CGFloat statusBarHeight = app.statusBarFrame.size.height;

 UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
 statusBarView.backgroundColor = [UIColor yellowColor];
 [self.navigationController.navigationBar addSubview:statusBarView];

Case 2: View without Navigation Bar

In this case use the following code in your viewDidLoad method

 UIApplication *app = [UIApplication sharedApplication];
 CGFloat statusBarHeight = app.statusBarFrame.size.height;

 UIView *statusBarView =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
 statusBarView.backgroundColor  =  [UIColor yellowColor];
 [self.view addSubview:statusBarView];

Source link http://code-ios.blogspot.in/2014/08/how-to-change-background-color-of.html

Solution 4

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

UPDATE:
also check developers-guide-to-the-ios-7-status-bar

Solution 5

You can 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:

enter image description here


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:

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.
  2. In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate

  3. override preferredStatusBarStyle in your view controller.

-

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}
Share:
238,418
Rejeesh Rajan
Author by

Rejeesh Rajan

Updated on July 08, 2022

Comments

  • Rejeesh Rajan
    Rejeesh Rajan almost 2 years

    My current application runs on iOS 5 and 6.

    The navigation bar is having an orange color and the status bar is having a black background color with white text color. However, when I run the same application on iOS 7, I observe the status bar looks transparent with the same orange background color as the navigation bar and the status bar text color is black.

    Due to this I'm not able to differentiate between the status bar and the navigation bar.

    How do I make the status bar to look the same as it was in iOS 5 and 6, that is with black background color and white text color? How can I do this programmatically?

  • Muruganandham K
    Muruganandham K over 10 years
    you can change it to black or white
  • Johnykutty
    Johnykutty over 10 years
    this is not about seting a text color
  • Dejell
    Dejell over 10 years
    Gabriele, can you please provide code how to put 20px high view behind it?
  • Adam
    Adam over 10 years
    This has no effect (ios7, simulator). The "preferredStatusBarStyle" is never invoked.
  • Muruganandham K
    Muruganandham K over 10 years
    are you using xib?. if YES change status bar value in simulated metrics property
  • Adam
    Adam over 10 years
    Ah, I found the issue. Apple's UINavigationController grabs the notification - i.e. your answer is only for when the view controller is the top controller, there are no containers (no tab-bar, no navbar, etc).
  • Shahid Iqbal
    Shahid Iqbal over 10 years
    Your first two lines are correct.. But Last Line should be [navigationController.view addSubview:view]; It should be added inside UINavigationController's view not UINavigationBar's view as it will add view after 20 px of status bar not overlapping status bar.
  • ObjectiveTC
    ObjectiveTC about 10 years
    Special case when using Storyboard + NavigationController. Do #1 above. Next, create a subclass for UINavigationController (call it myNavController). In Storyboard, set the NavigationController's class to "myNavController". In myNavController.m, do #2 & #3 above. The method in #3 will now be called in your subclass (set a log or breakpoint to observe).
  • Joel Balmer
    Joel Balmer about 10 years
    Just mentioning, apple docs recommend this if check instead: if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1){} else {} cheers!
  • Fattie
    Fattie about 10 years
    Dejel, that's on Shahid's answer.
  • jpcguy89
    jpcguy89 almost 10 years
    This is ridiculously difficult to figure out, thank you for providing the elusive answer.
  • learner
    learner over 9 years
    In the info.plist I don't see how to Set Status bar style to UIStatusBarStyleLightContent. Or is it that I should not expect suggestions? Also is the value of type String?
  • learner
    learner over 9 years
    Also, inside which delegate method do I place the if block?
  • Shahid Iqbal
    Shahid Iqbal over 9 years
    @learner Goto info.plist and then select any row. You will see a + sign. Click on Plus sign and from drop down, you should see Status bar style Option. Select it. And paste UIStatusBarStyleLightContent as its value.
  • Shahid Iqbal
    Shahid Iqbal over 9 years
    @learner I am talking about application AppDelegate Class. Every app has an AppDelegate class.
  • learner
    learner over 9 years
    @ShahidIqbal I understand that but which method? You simple show an if-block. In which method do I place the if-block? The delegate has a number of methods. Anyway, that was the question. But I figured it out. Thanks for replying. and +1 for the help. The method is didFinishLaunchingWithOptions
  • lostintranslation
    lostintranslation over 9 years
    This does not account for rotation
  • Benjamin Piette
    Benjamin Piette over 9 years
    Yes this is even better to handle screen sizes and orientations. Also add something like this around this code: if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
  • user3344977
    user3344977 over 9 years
    Adding the subview was the only solution that worked for me when using a custom UINavigationBar in iOS 7 and 8. Thank you.
  • KlimczakM
    KlimczakM about 9 years
    Its better to use UIScreen width: UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];
  • Eneko Alonso
    Eneko Alonso about 9 years
    Also consider the status bar being 40 pixels when the phone is on call or tethering modes.
  • Doug
    Doug over 8 years
    A more succinct way to set the frame is using UIApplication.sharedApplication().statusBarFrame
  • Michael
    Michael over 8 years
    This works, but I don't think it's really the best way to handle this style
  • Michael
    Michael about 8 years
    Unlike the accepted solution, this also works when you change orientation. Thanks!
  • Robert J. Clegg
    Robert J. Clegg almost 8 years
    Works perfectly for me and the easiest solution.
  • Foriger
    Foriger almost 8 years
    Isn't that private API usage?
  • LordParsley
    LordParsley almost 8 years
    This worked well for me, but the status bar ought to be 20pt high: [[UIView alloc] initWithFrame:CGRectMake(0, -20, 320, 20)];
  • Sebyddd
    Sebyddd over 7 years
    I had one app rejected cause of this, while another was accepted just fine. They do consider it private API usage, so you are subject to luck during the reviewing process :)
  • Warif Akhand Rishi
    Warif Akhand Rishi over 7 years
    @Sebyddd That is bad news. I guess I was lucky all the time. Could you share us the error app store review team sent you. Did you need to deal with keyboard? What alternative approach did you take? Also I've added ur comment in the answer so people easily notice it. Thanks for sharing.
  • Sebyddd
    Sebyddd over 7 years
    The message is pretty general and doesn't help much. I posted it bellow. In my case the keyboard was not an issue. I ended up slipping a view beneath the status bar at window level (something similar to what @shahid-iqbal did in his answer). "We found that your app uses one or more non-public APIs, which is not in compliance with the App Store Review Guidelines. The use of non-public APIs is not permissible because it can lead to a poor user experience should these APIs change."
  • Warif Akhand Rishi
    Warif Akhand Rishi over 7 years
    I was hoping more form apple, like.. we don't want status bar bg changed.. or if u want to change bg do this.. :-( .. anyways.. thanks for the info @Sebyddd
  • JVS
    JVS over 7 years
    In Swift use this: let rect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIApplication.shared.statusBarFrame.height) let bar = UIView(frame: rect) bar.backgroundColor = UIColor.white navigationController?.view.addSubview(bar)
  • Mohammad Zaid Pathan
    Mohammad Zaid Pathan over 7 years
    Make sure to call it after setting rootViewController.
  • user431791
    user431791 over 7 years
    why can't you directory change the background view color instead of creating a new view.. like self.window?.rootViewController?.view.backgroundColor = UIColor.black
  • Fattie
    Fattie over 7 years
    One of the best answers on the site, thanks @WarifAkhandRishi. I put in a long answer based on yours, which may further save people typing!
  • Fattie
    Fattie over 7 years
    Don't just use "20" ! You can get the value correctly, see my long answer below.
  • kelin
    kelin over 7 years
    This answer is super cool! I was searching a way to hide/modify Status Bar on simulator for iTunes Connect promo screens. With direct access to the bar I can customize it at any way.
  • LightNight
    LightNight about 7 years
    Thanks for "UIViewControllerBasedStatusBarAppearance" key, helped me :)
  • Timeless
    Timeless about 7 years
    There is an issue with this solution, when you double press the home button, this status status bar color will disappear.
  • Warif Akhand Rishi
    Warif Akhand Rishi about 7 years
    @Timeless: Status bar is hidden from the iOS if you double press. So background color disappears.
  • halil_g
    halil_g about 7 years
    Adding a background image is an overkill if you just want to set a solid color, and infeasible especially if you want to be able to change the color on the fly
  • birdcage
    birdcage almost 7 years
    How to make a single view controller's status bar transparent if we use this code?
  • dengApro
    dengApro over 6 years
    So maybe it's better to change the UI backgroundColor by the view of the status bar position , nor the statusBar which is UIWindow controlled by the OS when you double press the home button. Like the answer(stackoverflow.com/questions/47250495/…), or other answer in this page.
  • abhimuralidharan
    abhimuralidharan over 6 years
    Will the app get rejected if we use these? Changing the statusBarView color like this is allowed?
  • Krunal
    Krunal over 6 years
    @abhi1992 I can't say whether apple will accept it or not because I implemented this solution in my enterprise application, which does not need to submit on App store. :)
  • Admin
    Admin over 6 years
    If I put this in a viewdidload of a viewcontroller in a tab-based app, it sets the color for every viewController, non only the one where I put the code (is this normal?)
  • Vladyslav Panchenko
    Vladyslav Panchenko about 5 years
    There is an issue with this solution, when you get a notification, status bar disappears too
  • NSDeveloper
    NSDeveloper almost 5 years
    Not work on iOS 13. App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.
  • Ahmadreza
    Ahmadreza almost 4 years
    What language is this?
  • Dan
    Dan almost 4 years
    @Alfi Xamarin forms and in the background its C#
  • kelin
    kelin almost 4 years
    You will get double status bar on iOS 13.
  • Gonçalo Gaspar
    Gonçalo Gaspar almost 4 years
    For me this was the best answer since it mitigated a lot of other answer issues. The downside is the fixed frame.size.height = 64 which is wrong. One more recent way of getting the height would be -> .view.window?.windowScene?.statusBarManager?.statusBarFrame.‌​height ?? 0.