The Other Way

There are times when a constraint cannot be created with a visual format string. For instance, you cannot use VFL to create a constraint based on a ratio, like if you wanted the date label to be twice as tall as the name label or if you wanted the image view to always be 1.5 times as wide as it is tall.

In these cases, you can create an instance of NSLayoutConstraint using the method

+​ ​(​i​d​)​c​o​n​s​t​r​a​i​n​t​W​i​t​h​I​t​e​m​:​(​i​d​)​v​i​e​w​1​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​a​t​t​r​i​b​u​t​e​:​(​N​S​L​a​y​o​u​t​A​t​t​r​i​b​u​t​e​)​a​t​t​r​1​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​r​e​l​a​t​e​d​B​y​:​(​N​S​L​a​y​o​u​t​R​e​l​a​t​i​o​n​)​r​e​l​a​t​i​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​o​I​t​e​m​:​(​i​d​)​v​i​e​w​2​ ...

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.