Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Created December 4, 2015 09:57
Show Gist options
  • Save onmyway133/faa135b4db601d0db9a8 to your computer and use it in GitHub Desktop.
Save onmyway133/faa135b4db601d0db9a8 to your computer and use it in GitHub Desktop.
UILayoutGuide_Container.swift
class ViewController: UIViewController {
let container = UILayoutGuide()
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Swift"
view.addSubview(label)
let textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.text = "Language"
view.addSubview(textField)
view.addLayoutGuide(container)
// Set interior constraints
label.lastBaselineAnchor.constraintEqualToAnchor(textField.lastBaselineAnchor).active = true
label.leadingAnchor.constraintEqualToAnchor(container.leadingAnchor).active = true
textField.leadingAnchor.constraintEqualToAnchor(label.trailingAnchor, constant: 8.0).active = true
textField.trailingAnchor.constraintEqualToAnchor(container.trailingAnchor).active = true
textField.topAnchor.constraintEqualToAnchor(container.topAnchor).active = true
textField.bottomAnchor.constraintEqualToAnchor(container.bottomAnchor).active = true
// Set exterior constraints
// The contents of the container can be treated as a black box
let margins = view.layoutMarginsGuide
container.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor).active = true
container.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor).active = true
// Must use NSLayoutConstraint with the scene's top and bottom layout guides.
NSLayoutConstraint(item: container,
attribute: .Top,
relatedBy: .Equal,
toItem: topLayoutGuide,
attribute: .Bottom,
multiplier: 1.0,
constant: 20.0).active = true
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print(container.layoutFrame)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment