12

I am trying to change the color of the status bar to like a blue, or some other color.

Is this possible, or does Apple not allow it?

Krunal
  • 77,632
  • 48
  • 245
  • 261
Tyler Rutt
  • 567
  • 2
  • 8
  • 24
  • 1
    Based on the two answers (with comments), the answer (like I thought) is **no**. You can (1) change the font color of a status bar or (2) use private APIs, but you are taking a chance of Apple rejecting your app (or worse, rejecting it somewhere down the road). –  Mar 28 '17 at 15:54

5 Answers5

16

NOTE: This solution fails under iOS 13 and later.

First in Plist set View controller-based status bar appearance to NO

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
    if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
        statusBar.backgroundColor = UIColor.blue
    }
    UIApplication.shared.statusBarStyle = .lightContent

    return true
}

The output screenshot is below

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3182143
  • 9,459
  • 3
  • 32
  • 39
  • 6
    This is using private, undocumented APIs that may not pass app review. It's never a good idea to do things like this. It's likely to crash, especially as written, when the private API changes in some future iOS update. – rmaddy Mar 28 '17 at 15:32
  • 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 – byJeevan Mar 28 '17 at 15:38
  • I would definitely **add** this fact to your answer. –  Mar 28 '17 at 15:38
  • @rmaddy You'll be happy to know that this code, originally written for (and happily working since) iOS 10, kills my app at runtime on iOS 13. – Jamie Birch Sep 25 '19 at 18:22
  • 1
    @JamieBirch It doesn't make me happy but it does verify why poking around private APIs is a bad idea. – rmaddy Sep 25 '19 at 18:26
9

No, it's not possible with ready-made public APIs.

But with the release of iOS 7, you’re allowed to change the appearance of the status bar. Hence I am posting my workaround.

From an individual view controller by overriding the preferredStatusBarStyle:

-(UIStatusBarStyle)preferredStatusBarStyle 
{ 
    return UIStatusBarStyleLightContent; 
}

Alternatively, you can set the status bar style by using the UIApplication statusBarStyle method. To do this, insert a new key named “View controller-based status bar appearance” and set the value to NO. enter image description here

By disabling the “View controller-based status bar appearance”, you can set the status bar style by using the following code.

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

At the end, change the UINavigationBar property tint color like below

[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
byJeevan
  • 3,728
  • 3
  • 37
  • 60
  • To clarify your answer, do this change the status bar **background** color to a **specific** color? –  Mar 28 '17 at 15:35
  • This edit makes no sense. The OP wants to change the status bar, not the navigation bar. –  Mar 28 '17 at 15:38
  • Kindly refer this link http://www.appcoda.com/customize-navigation-status-bar-ios-7/ – byJeevan Mar 28 '17 at 15:38
  • Sorry, but I'm not seeing what the OP is asking for. Changing the *style* of the status bar changes the **font**, not the background color. And the only other thing I find related to the status bar is hiding it. (I know this post from a few years ago. It's still a good post. But it doesn't answer the specific question of setting a background color of the status bar, which I believe cannot be done with a public API.) –  Mar 28 '17 at 15:43
  • @dfd I am totally agree with you. But, I answer shows a legal(apple) way to the solve problem. which I guess best possible solution. :-) – byJeevan Mar 28 '17 at 15:49
  • 1
    It doesn't actually solve the problem, it merely shows that limits to customizing the status bar. For this specific OP, the correct answer is "no, it's not possible with public APIs". –  Mar 28 '17 at 15:52
  • @dfd Thanks for suggestions, I have improved answer. :-D – byJeevan Mar 28 '17 at 15:58
6


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
}

Set value of .plist according to status bar style setup level. enter image description here

Krunal
  • 77,632
  • 48
  • 245
  • 261
4

Here is my workaround: create a UIView, add it to your root view of view controller as artificial status bar background
1.Create a UIView

// status bar's height is 20.0pt
CGRect frame = CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, 20.0);
UIView *fakeStatusBarBG = [[UIView alloc] initWithFrame:frame];
fakeStatusBarBG.backgroundColor = [UIColor yourColor];

2.Add it to your view controller's root view

// self is your view controller, make sure fakeStatusBarBG is the top subview in your view hierarchy
[self.view insertSubview:fakeStatusBarBG aboveSubview:yourTopSubview];

There your go.

3.(Additional)Change the content color on status bar, only white or black.

- (UIStatusBarStyle)preferredStatusBarStyle
{
    if (youWantWhiteColor)
    {
        return UIStatusBarStyleLightContent;
    }
    return UIStatusBarStyleDefault;
}

This workaround does not use private API, so you're safe. :-)

steveluoxin
  • 419
  • 4
  • 14
2

I made this extension to change color of status bar

public extension UIViewController {
    func setStatusBar(color: UIColor) {
        let tag = 12321
        if let taggedView = self.view.viewWithTag(tag){
            taggedView.removeFromSuperview()
        }
        let overView = UIView()
        overView.frame = UIApplication.shared.statusBarFrame
        overView.backgroundColor = color
        overView.tag = tag
        self.view.addSubview(overView)
    }
}

Here is usage anywhere in viewcontroller:

setStatusBar(color: .red)

Rifat Monzur
  • 640
  • 1
  • 5
  • 11