2

After set navigationBarHidden=YES, the statusBar becomes transparent.

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

enter image description here

ishwardgret
  • 1,068
  • 8
  • 10
Ran
  • 303
  • 1
  • 3
  • 15
  • usually I would create a top constraint and set the constraint constant to device status bar height. Noted iphonex has a different status bar height than others. – Surely May 08 '18 at 11:40
  • @Surely could you show me an example? – Ran May 08 '18 at 11:48
  • If you are using storyboard, you can set a top to superview constraint, and then drag this constraint to your view controller. In viewDidLoad, you can update the constraint's contant value to device status bar height. For example: topConstraint.constant = UIApplication.shared.statusBarFrame.height. If you are not using storyboard, you can set the top most view's frame's y value to the status bar height value. – Surely May 08 '18 at 15:36
  • "set the top most view's frame's y value to the status bar height value" if set the y value ,the status bar will be blocked by the view? @Surely – Ran May 09 '18 at 00:59
  • 2
    no the whole idea is to have your views below status bar. – Surely May 09 '18 at 01:22
  • Thank you @Surely Finally , I draw the view below the statusbar. As you say set y position and height – Ran May 09 '18 at 08:47

1 Answers1

1

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

Maziar Saadatfar
  • 1,286
  • 1
  • 5
  • 11
  • Well yeah, this allows us to colorize status bar. And it's also a hackish, we grab `statusBar` object using KVC. It's not problematic for this example (cause its known its 'valid' and won't crash on iOS 13 and less), but generally not good recommendable. – Whirlwind Mar 02 '22 at 20:38
  • Anyway, I haven't explained precisely what I was trying to achive. I will accept this, cause its a possible solution anyway. I will make another question where I will explain problem in detail, and post here a link if You want to peek (cause its actually similar but the the same is OP's original question) – Whirlwind Mar 02 '22 at 20:40
  • Here is the link to my question related to this : https://stackoverflow.com/q/71329167/3402095 – Whirlwind Mar 02 '22 at 21:10