2

Somehow, no matter the query or number of answers I come across, I can't find one person who can say how to set the status bar color in iOS using Swift 3. I've seen all the suggestions where you add:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

but the thing is, I don't want a transparent status bar. I want a specific hex color for the status bar color.

1
  • 1
    "I don't want a transparent status bar" Tough luck. It is transparent (in recent versions of iOS). Once you accept reality, you can start accepting workarounds. Commented Feb 3, 2017 at 22:37

2 Answers 2

3

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


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

Sign up to request clarification or add additional context in comments.

Comments

1

There isn’t an API to set the color of the status bar’s content; if you’d like one, you should file an enhancement request here. If you want to change the color of the background behind the status bar, put a view with that background color at the top of your app’s window.

1 Comment

Upvote for putting a colored view below status bar. Sadly that was my way of doing it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.