Daryl Wong
Daryl Wong

Reputation: 2443

iOS 13 setting status bar background color

I used to have the below code but after upgrading to iOS 13, I have got the error below:

UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 94/225, green: 64/255, blue:204/255, alpha: 1.0)

App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'

How could the background color of status bar be set now?

The warning message mention about using statusBarManager and so I did something like this, yet I could not get it to work.

var statusBar = UIView()

    if #available(iOS 13.0, *) {
        statusBar = UIView(frame: UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
        statusBar.backgroundColor = UIColor.red
        UIApplication.shared.keyWindow?.addSubview(statusBar)
    } else {
        //ios 12 and below
        UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 94/225, green: 64/255, blue:204/255, alpha: 1.0)
    }

Upvotes: 2

Views: 3715

Answers (3)

Nilay Dagdemir
Nilay Dagdemir

Reputation: 284

For the statusBarFrame deprecated warning, updated answer of the Daryl Wong's accepted answer:

 let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
 guard let statusBarHeight = window?.windowScene?.statusBarManager?.statusBarFrame.size.height else { return }
 let statusbarView = UIView(frame: CGRect(x: 0,
                                          y: 0,
                                          width: UIScreen.main.bounds.size.width,
                                          height: statusBarHeight))
 statusbarView.backgroundColor = UIColor.red
 view.addSubview(statusbarView)

Upvotes: 0

JaspreetKour
JaspreetKour

Reputation: 837

Code works for Swift 5+ and iOS 13+

Please Refer to answer : https://stackoverflow.com/a/64924164/7610245

Added "statusBarColorChange()" to UIViewController extension.

func statusBarColorChange() {

if #available(iOS 13.0, *) {

    let statusBar = UIView(frame: UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
    statusBar.backgroundColor = .appThemeButtonsColor
        statusBar.tag = 100
    UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.addSubview(statusBar)

} else {

        let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
        statusBar?.backgroundColor = .appThemeButtonsColor

    }
}

Hope will be helping :)

Upvotes: 0

Daryl Wong
Daryl Wong

Reputation: 2443

Kuray just provided me a solution here:

https://freakycoder.com/ios-notes-13-how-to-change-status-bar-color-1431c185e845

Add the below to viewdidload. Please head over to his medium post and give him a few claps!

if #available(iOS 13.0, *) {
        let app = UIApplication.shared
        let statusBarHeight: CGFloat = app.statusBarFrame.size.height

        let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
        statusbarView.backgroundColor = UIColor.red
        view.addSubview(statusbarView)
    } else {
        let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
        statusBar?.backgroundColor = UIColor.red
    }

Upvotes: 6

Related Questions