Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,018 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 :/