2

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)
    }
Daryl Wong
  • 2,023
  • 5
  • 28
  • 61
  • Did you try to use `statusBarManager`? – vpoltave Sep 26 '19 at 09:50
  • I would mark this as a duplicate of https://stackoverflow.com/questions/56651245/how-to-change-the-status-bar-background-color-and-text-color-on-ios-13 but that doesn't have a single valid answer. – rmaddy Sep 26 '19 at 15:09

3 Answers3

6

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
    }
Daryl Wong
  • 2,023
  • 5
  • 28
  • 61
0

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

JaspreetKour
  • 777
  • 9
  • 11
0

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)
Nilay Dagdemir
  • 236
  • 2
  • 15