Last active
January 25, 2018 03:46
-
-
Save aainaj/1380d913a88b56c68073fb5acad1bc4d to your computer and use it in GitHub Desktop.
Safe area layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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