Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the Color of the Status Bar [duplicate]

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?

like image 822
Tyler Rutt Avatar asked Mar 28 '17 15:03

Tyler Rutt


4 Answers

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)

like image 188
Rifat Monzur Avatar answered Oct 26 '22 23:10

Rifat Monzur


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

like image 44
user3182143 Avatar answered Oct 26 '22 21:10

user3182143


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]];
like image 36
byJeevan Avatar answered Oct 26 '22 22:10

byJeevan



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

like image 39
Krunal Avatar answered Oct 26 '22 21:10

Krunal