Skip to content

Instantly share code, notes, and snippets.

@onurgenes
Created July 25, 2018 12:29
Show Gist options
  • Select an option

  • Save onurgenes/4bc63cd9e824f93c7f3aefe70eebcd1d to your computer and use it in GitHub Desktop.

Select an option

Save onurgenes/4bc63cd9e824f93c7f3aefe70eebcd1d to your computer and use it in GitHub Desktop.
import UIKit
class ViewController: UIViewController {
var button: UIButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("dasdasdas", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = UIColor.black
//left edge
let leftEdgeConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.left, multiplier: 1.0, constant: 0)
//right edge
let rightEdgeConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.right, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.right, multiplier: 1.0, constant: 0)
// bottom edge
let topEdgeConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 10)
// add all constraints
view.addConstraints([leftEdgeConstraint,rightEdgeConstraint,topEdgeConstraint])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment