The statusBar becomes transparent, After set navigationBar Hidden

180 Views Asked by At

After set navigationBarHidden=YES, the statusBar becomes transparent.

The only way is set statusBar backgroundcolor=white? (Actually it works...)

enter image description here

1

There are 1 best solutions below

3
Maziar Saadatfar On

You can use below trick:

func setStatusBarColor() {
        if #available(iOS 13, *)
        {
            let keyWindow = UIApplication.shared.connectedScenes
                    .filter({$0.activationState == .foregroundActive})
                    .compactMap({$0 as? UIWindowScene})
                    .first?.windows
                    .filter({$0.isKeyWindow}).first
            let statusBar = UIView(frame: (keyWindow?.windowScene?.statusBarManager?.statusBarFrame) ?? CGRect(x: 0, y: 0, width: screenWidth, height: statubarHeight))
            statusBar.backgroundColor = .white //your color
            keyWindow?.addSubview(statusBar)
        } else {
            let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
            if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
                statusBar.backgroundColor = .white //your color
            }
            UIApplication.shared.statusBarStyle = .lightContent
        }
    }

and also you can use:

yourController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true