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!

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 Using Layout Anchors

Sheng Wei
Sheng Wei
4,382 Points

[Help!] How to create view constraint?

Please help! Im not sure what is wrong with my code, thank you

layout.swift
class MyViewController: UIViewController {

    let blueView = UIView()
    let greenView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(greenView)
        view.addSubview(blueView)
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        blueView.translatesAutoresizingMaskIntoConstraints = false
        greenView.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activateConstraints([
            NSLayoutConstraint(item: blueView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100.0),
            NSLayoutConstraint(item: blueView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 150.0),
            NSLayoutConstraint(item: blueView, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .LeftMargin, multiplier: 1.0, constant: 8.0),
            NSLayoutConstraint(item: blueView, attribute: .Top, relatedBy: .Equal, toItem: self.topLayoutGuide, attribute: .Bottom, multiplier: 1.0, constant: 8.0),
            NSLayoutConstraint(item: blueView, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .RightMargin, multiplier: 1.0, constant: -8.0)
        ])

        // Add constraint code below
        NSLayoutConstraint.activateConstraints([
            NSLayoutConstraint(item: greenView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 75.0),
            NSLayoutConstraint(item: greenView, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .LeftMargin, multiplier: 1.0, constant: 8.0),
            NSLayoutConstraint(item: greenView, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .RightMargin, multiplier: 1.0, constant: 8.0),
            greenView.bottomAnchor.constraintEqualToAnchor(blueView.bottomAnchor, constant: 8.0)
            ])
    }
}

1 Answer

Greg Kaleka
Greg Kaleka
39,020 Points

Hi Sheng,

Check out the error message you get with your code: the right margin should be -8 points. Right now you have +8 points. Simply add a - in the right margin's value and you'll be all set.

Cheers :beers:

-Greg

Sheng Wei
Sheng Wei
4,382 Points

Awesome! Thanks so much Greg. Would you mind answering my question on the same module about constraints as well? I posted it a few days back