Khushal iOS
Khushal iOS

Reputation: 311

UIApplication.shared.keyWindow?.addSubview not working

UIApplication.shared.keyWindow?.addSubview not working in ios13

i try many options but not find any solutions. i want every screen status bar make blue color form AppDelegate .

i try this code.

if #available(iOS 13.0, *) {
         /*  let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
           // Reference - https://stackoverflow.com/a/57899013/7316675
           let statusBar = UIView(frame: window?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
           statusBar.backgroundColor = publicMethod().hexStringToUIColor(hex: "#033d6f")
           window?.addSubview(statusBar)
         */
        let statusBar =  UIView()
        statusBar.frame = UIApplication.shared.statusBarFrame
        statusBar.backgroundColor = .blue
        UIApplication.shared.statusBarStyle = .lightContent
        UIApplication.shared.keyWindow?.addSubview(statusBar)

    } else {
          let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
           if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
               //statusBar.backgroundColor = UIColor.darkGray
               statusBar.backgroundColor = publicMethod().hexStringToUIColor(hex: "#033d6f")
           }
    }

here is screenshot for this issue. enter image description here

Upvotes: 2

Views: 857

Answers (1)

Sajeka
Sajeka

Reputation: 11

You have probably already solved this question by now, but just in case someone else stumbles upon it in the future, I'll answer it with the code I got working.

First of, since you want to run this piece of code from the AppDelegate, I'm assuming you are running it from the didFinishLaunchingWithOptions.

In that case make sure that you don't run the code until after you have called makeKeyAndVisible() or else the keyWindow will be nil.

However, when you call addSubview() from didFinishLaunchingWithOptions the viewControllers will be added on top of it, and as such it might not be visible (at least that was the case for me, while using a navigationController).

To test this, when running the app click on the Debug View Hierarchy button in xCode as shown in the picture.

enter image description here

You can drag the screen to see the view hierarchy from different angles like in this picture:

enter image description here

If that is the case, you can make the statusBar the topmost view by setting the zPosition: statusBar.layer.zPosition = .greatestFiniteMagnitude

So my full code for this is:

func setStatusBar() {
    if #available(iOS 13.0, *) {
        let statusBar = UIView(frame: UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
        statusBar.backgroundColor = .blue
        UIApplication.shared.keyWindow?.addSubview(statusBar)
        statusBar.layer.zPosition = .greatestFiniteMagnitude
    } else {
        let statusbar = UIApplication.shared.value(forKey: "statusBar") as? UIView
        statusbar?.backgroundColor = .blue
    }
}

Again, this will not work in iOS 13 if this function is called before makeKeyAndVisible()

Upvotes: 0

Related Questions