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

Please Help! I can't seem to figure out where I'm wrong in this challenge

In the editor you've been provided with a UIViewController subclass that contains an instance of UIView, sampleView, as a stored property. You need to position and size sampleView by adding the following constraints: Height constraint of 50 points Width constraint of 50 points Center horizontally and vertically in container

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
            ])
    }

}
Natalia Khachapuridze
Natalia Khachapuridze
5,579 Points

Hey Arnold, I think that will work. If the issue is that you cant see the sample view it's because right now it has no colour. you could add a colour to test it:

override func viewDidLoad() { super.viewDidLoad()

    view.addSubview(sampleView)
    sampleView.backgroundColor = .red
}
Natalia Khachapuridze
Natalia Khachapuridze
5,579 Points

Although I just had a look again and when you create width and height constraints you need to put attribute: .notAnAttribute instead of attribute: .height That works

class MyViewController: UIViewController {

let sampleView = UIView()

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

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

    let sampleViewHeightConstraint  = NSLayoutConstraint(item: sampleView, attribute: .height, relatedBy: .equal,
                                                        toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50)
    let sampleViewWidthConstraint   = NSLayoutConstraint(item: sampleView, attribute: .width, relatedBy: .equal,
                                                         toItem: nil, attribute: .notAnAttribute, 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
        ])
}

}

1 Answer

Natalia was correct that you need to provide .notAnAttribute for the second attribute parameter in the height and width constraints; however, you'll also want to create a new method for setting up your constraints and call it separately instead of placing them in viewWillLayoutSubviews() (see setupSampleViewConstraints() method in the code below).

Hope that helps anyone who's having trouble with this!

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])
    }
}