【问题标题】:UIView not covering status bar areaUIView 不覆盖状态栏区域
【发布时间】:2018-03-30 22:45:14
【问题描述】:

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

我正在尝试使侧边菜单(灰色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

【问题讨论】:

  • 感谢@Sh_Khan 的回答,修复了错误。将 centerYconstraint 改为 bottomConstraint:sideBarUIView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true

标签: ios swift xcode uiview statusbar


【解决方案1】:

你可以试试这个

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


    }

}

【讨论】:

  • 不幸的是,这并没有奏效,但它确实让我知道了如何解决它。我已将sideBarUIView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true 更改为sideBarUIView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true,这似乎已经解决了。非常感谢你的想法!
  • 快乐编码 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-08
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 1970-01-01
  • 1970-01-01
  • 2013-09-26
相关资源
最近更新 更多