Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Created December 4, 2015 09:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onmyway133/fcf892aac565d36ee890 to your computer and use it in GitHub Desktop.
Save onmyway133/fcf892aac565d36ee890 to your computer and use it in GitHub Desktop.
UILayoutGuide_Spacing.swift
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Buttons
let saveButton = UIButton()
saveButton.translatesAutoresizingMaskIntoConstraints = false
saveButton.setTitle("Save", forState: .Normal)
saveButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
view.addSubview(saveButton)
saveButton.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor).active = true
saveButton.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
let cancelButton = UIButton()
cancelButton.translatesAutoresizingMaskIntoConstraints = false
cancelButton.setTitle("Cancel", forState: .Normal)
cancelButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
view.addSubview(cancelButton)
cancelButton.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor).active = true
let clearButton = UIButton()
clearButton.translatesAutoresizingMaskIntoConstraints = false
clearButton.setTitle("Clear", forState: .Normal)
clearButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
view.addSubview(clearButton)
clearButton.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor).active = true
clearButton.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true
// Constraints
let space1 = UILayoutGuide()
view.addLayoutGuide(space1)
let space2 = UILayoutGuide()
view.addLayoutGuide(space2)
space1.widthAnchor.constraintEqualToAnchor(space2.widthAnchor).active = true
saveButton.trailingAnchor.constraintEqualToAnchor(space1.leadingAnchor).active = true
cancelButton.leadingAnchor.constraintEqualToAnchor(space1.trailingAnchor).active = true
cancelButton.trailingAnchor.constraintEqualToAnchor(space2.leadingAnchor).active = true
clearButton.leadingAnchor.constraintEqualToAnchor(space2.trailingAnchor).active = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment