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

Matthew Foster
Matthew Foster
7,442 Points

Introduction to Auto Layout final challenge uses completely different syntax than what the videos and Xcode 7.3.1

Are you supposed to do this challenge and differently than just copying the example code?

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.activate([
            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


    }
}
Matthew Foster
Matthew Foster
7,442 Points

I got it to work but I could have done exactly the same without watching any of the videos because they had no bearing on the answers for this challenge.

1 Answer

Hi Mathew,

At the beginning of the course it is noted that the content may be outdated due to the release of Xcode 8 and Swift 3.

Although the syntax is different, it is conceptually the same and is important to know how to create and apply constraints to view programatically. You will go back over this topic when creating the 'Interactive Story App' in the 'Intermediate iOS Development' track.

It is good practice and beneficial to your knowledge to figure things out yourself anyhow, so congrats!

All the best,

Mitch