evenwerk
evenwerk

Reputation: 987

iOS - View above all other views with Safe Area

I would like to put a UIView above all other views. I saw that this is possible using the UIScreen object. However, it looks like the UIScreen doesn't have a Safe Area. This let's the View overlap with the Statusbar.

How can I place a View above all other Views while still using the Safe Area Insets?

I'm using this code in the Appdelegate's 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.

Upvotes: 0

Views: 1024

Answers (0)

Related Questions