Skip to content

Instantly share code, notes, and snippets.

@genedelisa
Last active November 12, 2016 11:47
Show Gist options
  • Save genedelisa/12819f1c4c9428c5348b to your computer and use it in GitHub Desktop.
Save genedelisa/12819f1c4c9428c5348b to your computer and use it in GitHub Desktop.
Programmatic autolayout constraints
// the frame size doesn't matter; autolayout will change it
var aButton = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
aButton.titleLabel?.text = "HEY"
aButton.opaque = true
aButton.backgroundColor = UIColor.yellow
view.addSubview(aButton)
aButton.translatesAutoresizingMaskIntoConstraints = false
let margins = view.layoutMarginsGuide
aButton.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor, constant: -0).active = true
aButton.bottomAnchor.constraintEqualToAnchor(bottomLayoutGuide.topAnchor, constant: -30).active = true
aButton.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor, constant: 8.0).active = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment