UIView不覆盖状态栏区域

UIView not covering status bar area

我正在以编程方式创建汉堡菜单。

我正在尝试使侧边菜单(灰色 UIView)在滑出时覆盖状态栏区域,并且 UIView 具有屏幕大小的限制。

我在手势开始时隐藏了状态栏 alpha,但由于某种原因,状态栏的背景仍然是纯白色。请参见下面的示例:

是否可以让一个 UIView 覆盖这个区域?

边栏代码:

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

我正在使用这一行来隐藏状态栏:

self.statusBarWindow?.alpha = 0.0

你可以试试这个

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


    }

}