1
votes

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)
    }
2
Did you try to use statusBarManager?vpoltave
I would mark this as a duplicate of stackoverflow.com/questions/56651245/… but that doesn't have a single valid answer.rmaddy

2 Answers

5
votes

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
    }
0
votes

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 :)