14

I'm trying to build my app with Xcode 11 beta 6 and iOS 13 beta 8 but it throws this error once it starts to run:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '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.'

What is the window scene and how do I use the statusBarManager?
And I'm not sure if this is relevant, but I'm not using any SwiftUI.

Lukas Würzburger
  • 6,207
  • 7
  • 35
  • 68

2 Answers2

9

You need to add the following extension to access statusbarview :

extension UIApplication {
    var statusBarUIView: UIView? {
        if #available(iOS 13.0, *) {
            let tag = 38482458385
            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
    }
}
Manav
  • 1,284
  • 9
  • 21
  • above code doesn't solved the problem and app is still crashing, did you find any working solution ? – prateek sharma Sep 30 '19 at 15:09
  • 1
    My code was `guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }` . I have applied above code `guard let statusBar = UIApplication.shared.statusBarUIView else { return }` and it is working fine. – Komal Goyani Oct 03 '19 at 09:17
1

To access StatusBar in Swift 5+ and iOS 13+

Need to replaced -satusBar or -statusWindow with statusBarManager.

if #available(iOS 13.0, *) {

        let statusBar = UIView(frame: UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
        statusBar.backgroundColor = .appNavigationThemeColor
          //  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 = .appNavigationThemeColor

        }
    }

Hope will be helping :)

JaspreetKour
  • 607
  • 8
  • 9