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

Sheng Wei
Sheng Wei
4,382 Points

[Help!] Whats wrong with my constraints?

Hi, I would appreciate if someone could point out what the error is in my syntax for constraints. Thanks.

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

        func setUpsampleViewConstraints() { 
            sampleView.translatesAutoresizingMaskIntoConstraints = false    

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

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

            view.addConstraints([
              sampleViewHeightConstraint,
              sampleViewWidthConstraint])

    }
}

3 Answers

Craig Salmond
PLUS
Craig Salmond
Courses Plus Student 7,438 Points

The second attributes for sampleViewHeightConstraint and sampleViewWidthConstraint needs to be .NotAnAttribute, not .CenterX. Also you don't have the a constraint to vertically center the sampleView.

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Sheng,

You're missing a closing curly brace on your setUpsampleViewConstraints function. I think you also need to center the view vertically, per the instructions. Hopefully this is enough to get you through.

Cheers :beers:

-Greg

Sheng Wei
Sheng Wei
4,382 Points

Hi Greg,

This is my new code as per your advice:

func setUpsampleViewConstraints() { 
            sampleView.translatesAutoresizingMaskIntoConstraints = false    
            }

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

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

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

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

The Bummer! I keep getting is: Don't forget to add the constraints to the parent view!

Not sure what's missing here :/