iOS - 使用安全区域查看所有其他视图的上方

时间:2018-03-01 11:07:58

标签: ios swift uiscreen safearealayoutguide

我想把UIView放在所有其他视图之上。我看到使用UIScreen对象可以实现这一点。但是,看起来UIScreen没有安全区域。这是视图与状态栏的重叠。

如何在仍使用安全区域插入的同时将视图放在所有其他视图上方?

我在Appdelegate的DidFinishLaunchingWithOptions()中使用此代码:

window?.makeKeyAndVisible()

let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.red
window?.addSubview(view)
window?.bringSubview(toFront: view)

let heightConstraint = NSLayoutConstraint(item: view, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 44)
view.addConstraint(heightConstraint)

let layoutGuide = window!.safeAreaLayoutGuide
let layoutGuideConstraints = [
    view.topAnchor.constraint(equalTo: layoutGuide.topAnchor, constant: 8),
    view.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor, constant: 8),
    view.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor, constant: -8)
]
NSLayoutConstraint.activate(layoutGuideConstraints)

Example of the executed code.

0 个答案:

没有答案