Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change Status Bar Alpha in iOS 13?

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

like image 569
Benjamin Hutter Avatar asked Jul 17 '19 20:07

Benjamin Hutter


People also ask

Can we change status bar color in IOS?

Select the View and in the Attributes Inspector change the Background Color to Light Gray. Build and Run the Project. The default style of the status bar is dark content. The style of the status bar can be changed to a status bar with white content.

What is the IOS status bar?

A status bar appears along the upper edge of the screen and displays information about the device's current state, like the time, cellular carrier, and battery level.


Video Answer


3 Answers

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.

like image 96
Akshar Darji Avatar answered Oct 17 '22 18:10

Akshar Darji


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.

like image 20
Douglas Hill Avatar answered Oct 17 '22 17:10

Douglas Hill


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
}
like image 1
FreakyCoder Avatar answered Oct 17 '22 18:10

FreakyCoder