Skip to content

Instantly share code, notes, and snippets.

@aainaj
Last active January 25, 2018 03:46
Show Gist options
  • Save aainaj/1380d913a88b56c68073fb5acad1bc4d to your computer and use it in GitHub Desktop.
Save aainaj/1380d913a88b56c68073fb5acad1bc4d to your computer and use it in GitHub Desktop.
Safe area layout
class ViewController: UIViewController {
let wrapperView = UIView()
let cyanView = UIView()
override func viewDidLoad() {
view.backgroundColor = .yellow
view.addSubview(wrapperView)
wrapperView.addSubview(cyanView)
wrapperView.backgroundColor = .red
wrapperView.translatesAutoresizingMaskIntoConstraints = false
cyanView.backgroundColor = .cyan
cyanView.translatesAutoresizingMaskIntoConstraints = false
applyConstraints()
}
func applyConstraints() {
let guide = view.safeAreaLayoutGuide
wrapperView.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
wrapperView.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
wrapperView.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
wrapperView.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
let margins = wrapperView.layoutMarginsGuide
cyanView.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
cyanView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
cyanView.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
cyanView.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment