Said-Abdulla Atkaev
Said-Abdulla Atkaev

Reputation: 4323

Status bar background color does not change to custom color

When I change status bar background color to native UIColor.gray it changes. But when I want to use custom color it turn black color.

UIApplication.shared.statusBarView?.backgroundColor = UIColor.gray - this code workes correct. Status bar background color is gray

UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 30/255, green: 30/255, blue: 30/255, alpha: 1) - this code workes incorrect. Status bar background color is black

Upvotes: 2

Views: 6816

Answers (3)

Inder Jagdeo
Inder Jagdeo

Reputation: 229

First of all, set View controller-based status bar appearance property No in info.plist file.

Then add the following code in didFinishLaunchingWithOptions method of AppDelegate Class.

extension UIApplication {
var statusBarView: UIView? {
    if #available(iOS 13.0, *) {
        let tag = 5111
        if let statusBar = self.keyWindow?.viewWithTag(tag) {
            return statusBar
        } else {
            let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
            statusBarView.tag = tag

            self.keyWindow?.addSubview(statusBarView)
            return statusBarView
        }
    } else {
        if responds(to: Selector(("statusBar"))) {
            return value(forKey: "statusBar") as? UIView
        }
    }
    return nil}
}

I hope this would help you.

Upvotes: 8

Krunal
Krunal

Reputation: 79636

May this help you in Swift 4. Looks like a hacky trick but works.

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

Upvotes: 5

Ahsan Ebrahim Khatri
Ahsan Ebrahim Khatri

Reputation: 1847

The code isnt incorrect, just that the color you are providing is dark gray itself. (R:30,G:30,B:30) represents dark gray and thats what is being used in the status bar, change this color to some other color for example (R:202,G:0,B:42) and it will display a red color.

Try

UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 202/255, green: 0/255, blue: 42/255, alpha: 1)

Upvotes: 0

Related Questions