5

I want to change the Status Bar Alpha in iOS 13.

let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow
statusBarWindow?.alpha = 0.5

When I try this, the app crashes (Thread 1: signal SIGABRT).

Benjamin Hutter
  • 121
  • 1
  • 3
  • 1
    As far as I know, you cannot do this. The status bar can either be black or white. You are trying to access a private API that I think Apple would not allow if you are submitting to the App Store. – Chris Jul 17 '19 at 20:42

3 Answers3

8

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.'

You can refer to the following answer to resolve your crash.

var statusBarUIView: UIView? {
        if #available(iOS 13.0, *) {
            let tag = 38482458
            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
    }

These mentioned solutions worked for me.

Akshar Darji
  • 397
  • 2
  • 12
  • 1
    You are just adding empty UIView in case of iOS 13, how this will work with StatusBar specific properties like eg. "foregroundColor" ? – ViruMax Sep 16 '19 at 03:37
  • 1
    Here I have only provided the solution for crashing, I guess we'll do change color and it's alpha from that `statusBarUIView` var. – Akshar Darji Sep 20 '19 at 06:17
  • @AksharDarji `keyWindow` is deprecated in iOS 13. – Parth Adroja Oct 22 '19 at 06:59
  • @ParthAdroja True. However, we have tested in beta and at that time it was working fine. Anyways if you want to use it, you can check on https://stackoverflow.com/a/57169802/2677134 or you can contribute here for the same. – Akshar Darji Oct 22 '19 at 08:08
  • You are just adding empty UIView in case of iOS 13 – Habib Ali Nov 26 '19 at 13:48
7

The status bar is rendered by a system process (out of the app process) on iOS 13 so there is no way for an app to change this. (Source: speaking to UIKit engineers at the WWDC 2019 labs.)

You can see this in the Apple Books app, which used to dim the status bar in its dark mode on iOS 12, but this does not happen on iOS 13.

Douglas Hill
  • 1,537
  • 10
  • 14
1

I have a custom solution for changing status bar on iOS 13 and below. Here is how to do that:

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

   let statusbarView = UIView()
   statusbarView.backgroundColor = UIColor.red
   view.addSubview(statusbarView)

   statusbarView.translatesAutoresizingMaskIntoConstraints = false
   statusbarView.heightAnchor
     .constraint(equalToConstant: statusBarHeight).isActive = true
   statusbarView.widthAnchor
     .constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
   statusbarView.topAnchor
     .constraint(equalTo: view.topAnchor).isActive = true
   statusbarView.centerXAnchor
     .constraint(equalTo: view.centerXAnchor).isActive = true

} else {
      let statusBar = UIApplication.shared.value(forKeyPath: 
   "statusBarWindow.statusBar") as? UIView
      statusBar?.backgroundColor = UIColor.red
}

Gist

Also, check the article iOS 13 How to Change StatusBar Color?

One last thing, you can still change statusbar style with :

 override var preferredStatusBarStyle : UIStatusBarStyle {
    return UIStatusBarStyle.lightContent
    //return UIStatusBarStyle.default   // Make dark again
}
FreakyCoder
  • 840
  • 11
  • 24