Changing the Color of the Status Bar [duplicate] Changing the Color of the Status Bar [duplicate] swift swift

Changing the Color of the Status Bar [duplicate]


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


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



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 barclass 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 statusbarclass 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