PennyWise
PennyWise

Reputation: 647

How to dim status bar in iOS 13

I have been working on an iOS app that, before iOS 13, was able to 'darken" the status bar when a pop-up appeared. This was simply done by adding a view with bounds the as the status bar and applying a .5 alpha to it:

lazy var fadeStatusBar: UIView = {
    let fadeStatusBar = UIView(frame: UIApplication.shared.statusBarView?.frame ?? UIApplication.shared.statusBarFrame)
    fadeStatusBar.backgroundColor = UIColor.black
    fadeStatusBar.alpha = 0
    fadeStatusBar.translatesAutoresizingMaskIntoConstraints = false
    return fadeStatusBar
}()

Where statusBarView is an extension of UIApplication:

extension UIApplication {
    var statusBarView: 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
    }
}

And then I could animate the fadeStatusBar alpha property to .5. However, this does not work anymore in iOS 13. I am merely wondering if it is at all still possible to accomplish, because I read in another thread that native iOS apps have removed this ability as well:

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.

My question differs from the one referenced above, though, because the question there is to edit the alpha of the status bar itself, while I would be glad to use an overlay view instead. Any suggestion to achieve the desired result would be appreciated.

Upvotes: 0

Views: 568

Answers (2)

Nandish
Nandish

Reputation: 1162

You can add the view and make it dim and add it as a sub view to status Bar as below :

// Dim status bar
let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
let wFrame = window?.windowScene?.statusBarManager?.statusBarFrame
let statusBarView = UIView(frame: wFrame!)
statusBarView.restorationIdentifier = "statusBarView"
statusBarView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
window?.addSubview(statusBarView)

If you want to revert it back you can do as below:

// Revert status bar back to normal
let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
for subview in window!.subviews {
    if subview.restorationIdentifier == "statusBarView" {
        subview.removeFromSuperview()
    }
}

Upvotes: 1

rantingmong
rantingmong

Reputation: 103

You can try creating another UIWindow instance and setting its level property to either statusBar or alert.

Linking Apple's documentation about UIWindow levels: https://developer.apple.com/documentation/uikit/uiwindow/level/

Upvotes: 0

Related Questions