Search code examples
iosswiftxcodeuiviewstatusbar

UIView not covering status bar area


I am creating a hamburger menu programmatically.

I am trying to make the side menu (grey UIView) cover the status bar area when it is slid out and the UIView has constraints that are the size of the screen.

I am hiding the status bar alpha when the gesture begins but for some reason, the background of the status bar is still solid white. See example below:

left screen, menu bar hidden - right screen, menu bar visible

Is it possible to make a UIView cover this area?

Code for the sidebar:

let screenHeight = UIScreen.main.bounds.height
let screenWidth = UIScreen.main.bounds.width

var sideBarUIView: UIView! = {
        let sideBarUIView = UIView()
        sideBarUIView.backgroundColor = UIColor(red:1.0, green:1.0, blue:0.21, alpha:0.0)
        sideBarUIView.translatesAutoresizingMaskIntoConstraints = false
        return sideBarUIView
    }()

 override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(sideBarUIView)
}

        sideBarUIView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        sideBarUIView.heightAnchor.constraint(equalToConstant: screenHeight).isActive = true
        sideBarUIView.widthAnchor.constraint(equalToConstant: screenWidth).isActive = true

I am using this line to hide the status bar:

self.statusBarWindow?.alpha = 0.0

Solution

  • You can try this

    class ViewController: UIViewController {
    
        let screenHeight = UIScreen.main.bounds.height
        let screenWidth = UIScreen.main.bounds.width
    
        lazy var sideBarUIView: UIView! = {
            let sideBarUIView = UIView()
            sideBarUIView.backgroundColor = UIColor.red
            sideBarUIView.translatesAutoresizingMaskIntoConstraints = false
            return sideBarUIView
        }()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            view.addSubview(sideBarUIView)
    
            sideBarUIView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
            sideBarUIView.heightAnchor.constraint(equalToConstant: screenHeight).isActive = true
            sideBarUIView.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.5).isActive = true
            sideBarUIView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    
    
        }
    
    }