Adding Constraints

You now have two arrays of NSLayoutConstraint objects. However, these constraints will have no effect on the layout until you explicitly add them using the UIView method

-​ ​(​v​o​i​d​)​a​d​d​C​o​n​s​t​r​a​i​n​t​s​:​(​N​S​A​r​r​a​y​ ​*​)​c​o​n​s​t​r​a​i​n​t​s​

Which view should receive the addConstraints: message? Usually, the closest common ancestor of the views that are affected by the constraint. Here is a list of rules you can follow to determine which view you should add constraints to:

  • If a constraint affects two views that have the same superview (such as the constraint labeled A in Figure 16.1), then the constraint should be added to their superview.

  • If a constraint affects just one view (the ...

Get iOS Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.