Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Introduction to Auto Layout in iOS Auto Layout in Code Constraints in Code

Greg Kaleka
Greg Kaleka
39,021 Points

Adding Constraints in Code

Really not sure what the problem is here. I've created and added all four constraints, but I'm getting the error

Bummer! Make sure you have the correct constraints. One for height, one for width, one for CenterX and one for CenterY
layout.swift
class MyViewController: UIViewController {

    let sampleView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(sampleView)
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        sampleView.translatesAutoresizingMaskIntoConstraints = false

        // Add constraints below
        let sampleViewHeightConstraint  = NSLayoutConstraint(item: sampleView, attribute: .Height, relatedBy: .Equal,
                                          toItem: nil, attribute: .Height, multiplier: 1, constant: 50)
        let sampleViewWidthConstraint   = NSLayoutConstraint(item: sampleView, attribute: .Width, relatedBy: .Equal,
                                          toItem: nil, attribute: .Width, multiplier: 1, constant: 50)
        let sampleViewCenterXConstraint = NSLayoutConstraint(item: sampleView, attribute: .CenterX, relatedBy: .Equal,
                                          toItem: view, attribute: .CenterX, multiplier: 1, constant: 0)
        let sampleViewCenterYConstraint = NSLayoutConstraint(item: sampleView, attribute: .CenterY, relatedBy: .Equal,
                                          toItem: view, attribute: .CenterY, multiplier: 1, constant: 0)
        view.addConstraints([
          sampleViewHeightConstraint,
          sampleViewWidthConstraint,
          sampleViewCenterXConstraint,
          sampleViewCenterYConstraint
          ])
    }

}

Hey Greg!

Not sure if you completed this since you posted this question, but the only error that I see is that your multipliers and constant fields aren't doubles. i.e. (multiplier: 1.0, constant: 50.0)

Try that out and it should work!

        let sampleViewCenterXConstraint = NSLayoutConstraint(item: sampleView, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1.0, constant: 0)

        let sampleViewCenterYConstraint = NSLayoutConstraint(item: sampleView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1.0, constant: 0)

        let sampleViewHeightConstraint = NSLayoutConstraint(item: sampleView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50.0)

        let sampleViewWidthConstraint = NSLayoutConstraint(item: sampleView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50.0)

        view.addConstraints([
            sampleViewCenterXConstraint,
            sampleViewCenterYConstraint,
            sampleViewHeightConstraint,
            sampleViewWidthConstraint
            ])
    }
Greg Kaleka
Greg Kaleka
39,021 Points

Thanks Terrell. Unfortunately, that did not fix the problem.

withDoubles.swift
class MyViewController: UIViewController {

    let sampleView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(sampleView)
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        sampleView.translatesAutoresizingMaskIntoConstraints = false

        // Add constraints below
        let sampleViewHeightConstraint  = NSLayoutConstraint(item: sampleView, attribute: .Height, relatedBy: .Equal,
                                          toItem: nil, attribute: .Height, multiplier: 1.0, constant: 50.0)
        let sampleViewWidthConstraint   = NSLayoutConstraint(item: sampleView, attribute: .Width, relatedBy: .Equal,
                                          toItem: nil, attribute: .Width, multiplier: 1.0, constant: 50.0)
        let sampleViewCenterXConstraint = NSLayoutConstraint(item: sampleView, attribute: .CenterX, relatedBy: .Equal,
                                          toItem: view, attribute: .CenterX, multiplier: 1.0, constant: 0.0)
        let sampleViewCenterYConstraint = NSLayoutConstraint(item: sampleView, attribute: .CenterY, relatedBy: .Equal,
                                          toItem: view, attribute: .CenterY, multiplier: 1.0, constant: 0.0)
        view.addConstraints([
          sampleViewHeightConstraint,
          sampleViewWidthConstraint,
          sampleViewCenterXConstraint,
          sampleViewCenterYConstraint
          ])
    }

}

1 Answer

Chris Adamson
Chris Adamson
132,143 Points

This is a tricky one, you were close the attribute is .NotAnAttribute for the 2nd item in the constraint:

class MyViewController: UIViewController {

    let sampleView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(sampleView)
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        sampleView.translatesAutoresizingMaskIntoConstraints = false

        // Add constraints below
        let sampleViewHeightConstraint  = NSLayoutConstraint(item: sampleView, attribute: .Height, relatedBy: .Equal,
                                          toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50.0)
        let sampleViewWidthConstraint   = NSLayoutConstraint(item: sampleView, attribute: .Width, relatedBy: .Equal,
                                          toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50.0)
        let sampleViewCenterXConstraint = NSLayoutConstraint(item: sampleView, attribute: .CenterX, relatedBy: .Equal,
                                          toItem: view, attribute: .CenterX, multiplier: 1.0, constant: 0.0)
        let sampleViewCenterYConstraint = NSLayoutConstraint(item: sampleView, attribute: .CenterY, relatedBy: .Equal,
                                          toItem: view, attribute: .CenterY, multiplier: 1.0, constant: 0.0)

        view.addConstraints([sampleViewHeightConstraint,sampleViewWidthConstraint,sampleViewCenterXConstraint,sampleViewCenterYConstraint])
    }

}

Chris Adamson is correct just with the exception that — as of this time (Swift 4) — the member values provided for the 'attribute' parameters (e.g., attribute: .centerX) need to be lowerCamelCase. (Adding full code below for reference / ease.)

class MyViewController: UIViewController {

    let sampleView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(sampleView)
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        sampleView.translatesAutoresizingMaskIntoConstraints = false

        // Add constraints below
        setupSampleViewConstraints()
    }

    func setupSampleViewConstraints() {

        let sampleViewCenterXConstraint = NSLayoutConstraint(item: sampleView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0.0)  
        let sampleViewCenterYConstraint = NSLayoutConstraint(item: sampleView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1.0, constant: 0.0)
        let sampleViewHeightConstraint = NSLayoutConstraint(item: sampleView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50.0)
        let sampleViewWidthConstraint = NSLayoutConstraint(item: sampleView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50.0)

        view.addConstraints([sampleViewCenterXConstraint, sampleViewCenterYConstraint, sampleViewHeightConstraint, sampleViewWidthConstraint])
    }
}