O'Reilly logo

Cocoa Programming for OS X: The Big Nerd Ranch Guide by Aaron Hillegass, Adam Preble, Nate Chandler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating Layout Constraints Programmatically

You have seen how constraints can be created in Interface Builder, but they can also be added programmatically using the NSLayoutConstraint class. Above, you created the constraint pinning the left side of the scroll view the standard distance from the left side of its superview, the window’s content view, using the Add New Constraints popover in IB. You would create an equivalent constraint programmatically like this:

l​e​t​ ​s​c​r​o​l​l​V​i​e​w​:​ ​N​S​S​c​r​o​l​l​V​i​e​w​ ​=​ ​.​.​.​ l​e​t​ ​s​u​p​e​r​v​i​e​w​ ​=​ ​s​c​r​o​l​l​V​i​e​w​.​s​u​p​e​r​v​i​e​w​!​ l​e​t​ ​c​o​n​s​t​r​a​i​n​t​ ​=​ ​N​S​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​(​i​t​e​m​:​ ​s​c​r​o​l​l​V​i​e​w​,​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required