How to manually program constraints for a button

This button has no constraints on it. How can I manually code the constraints like I would do in a label so that the button fits dead center in the screen.

This is a picture of the button.

This is the view controller code.

 import UIKit

  class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

First, add as a subview - probably in viewDidLoad(). If you don’t do this first you get exceptions when manipulating constraints.

Next, stop the unexpected constraints from causing you trouble:

button.translatesAutoresizingMaskIntoConstraints = false

If you don’t do that then the button will have extra constraints that have been generated based on its autoresizing mask, and you probably don’t want those.
Next, generate and apply new constraints. You said you want the button right in the middle. There are two ways of doing this, with visual layout constraints and by relating the button to other objects. I’ll show you the second way and you can look up the first. Assuming the view to which you are adding the button is called “view”:

    var buttonConstraints : [NSLayoutConstraint] = []
    buttonConstraints.append(NSLayoutConstraint(item: view, attribute: .CenterX, relatedBy: .Equal, toItem: button, attribute: .CenterX, multiplier: 1.0, constant: 0.0))
    buttonConstraints.append(NSLayoutConstraint(item: view, attribute: .CenterY, relatedBy: .Equal, toItem: button, attribute: .CenterY, multiplier: 1.0, constant: 0.0))
    view.addConstraints(buttonConstraints)

I am confused about about subviews. This is what I wrote. Obviously it does not work.

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
@IBAction func thebutton(sender: AnyObject) {
    button.translatesAutoresizingMaskIntoConstraints = false
    var buttonConstraints : [NSLayoutConstraint] = []
    buttonConstraints.append(NSLayoutConstraint(item: view, attribute: .CenterX, relatedBy: .Equal, toItem: button, attribute: .CenterX, multiplier: 1.0, constant: 0.0))
    buttonConstraints.append(NSLayoutConstraint(item: view, attribute: .CenterY, relatedBy: .Equal, toItem: button, attribute: .CenterY, multiplier: 1.0, constant: 0.0))
    view.addConstraints(buttonConstraints)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

OK, I see from your code that you are probably adding your button in the storyboard. That is OK and is an alternative to creating the button in code and then manually adding it as a subview. By placing it on the view in the storyboard you are achieving the same thing.

You should not put your IBAction in viewDidLoad - that’s the function called when someone clicks on the button. Instead you should put just the lines

button.translatesAutoresizingMaskIntoConstraints = false
var buttonConstraints : [NSLayoutConstraint] = []
buttonConstraints.append(NSLayoutConstraint(item: view, attribute: .CenterX, relatedBy: .Equal, toItem: button, attribute: .CenterX, multiplier: 1.0, constant: 0.0))
buttonConstraints.append(NSLayoutConstraint(item: view, attribute: .CenterY, relatedBy: .Equal, toItem: button, attribute: .CenterY, multiplier: 1.0, constant: 0.0))
view.addConstraints(buttonConstraints)

in viewDidLoad. Of course you must create an IBOutlet for the button so you can refer to it, and you must use that reference (“button”).

Thanks man it worked