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 trialMarc Jones
3,751 PointsNSLayoutConstraint & ConstraintEqualToAnchor
Hi All,
Apologies if I missed this in the video. I'm going to re-watch in case I did, but when adding the layout constraints, how does it know what values to use? Or am I missing something obvious here?
override func viewWillLayoutSubviews() {
view.addSubview(artwork)
//turn off autolayout
artwork.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activateConstraints([
artwork.topAnchor.constraintEqualToAnchor(view.topAnchor),
artwork.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor),
artwork.rightAnchor.constraintEqualToAnchor(view.rightAnchor),
artwork.leftAnchor.constraintEqualToAnchor(view.leftAnchor)
])
}
Thanks, Marc
Abdulwahab Alansari
15,151 PointsThar raises a question, why the bottomAnchor of artwork does not equal the bottomAnchor of the view, the image didn't stretch to the bottom of the view
Hemanth C
8,970 PointsHemanth C
8,970 PointsIt is not using the values per se. Those 4 constraint added in NSLayoutConstraint basically says that artwork topAnchor/bottomAnchor/rightAnchor/leftAnchor should be equal to main view's topAnchor/bottomAnchor/rightAnchor/leftAnchor respectively.
view's topAnchor is the top edge of the layout guideβs frame.So artwork topAnchor have the same data has view's topAnchor. Similarly for others as well.