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​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​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​2​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​m​u​l​t​i​p​l​i​e​r​:​(​C​G​F​l​o​a​t​)​m​u​l​t​i​p​l​i​e​r​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​c​o​n​s​t​a​n​t​:​(​C​G​F​l​o​a​t​)​c​

This method creates a single constraint using two layout attributes of two view objects. The multiplier is the key to creating a constraint based on a ratio. The constant is a fixed number of points, like you have used in your spacing constraints.

The layout attributes are defined as constants in the NSLayoutConstraint class:

  • NSLayoutAttributeLeft

  • NSLayoutAttributeRight

  • NSLayoutAttributeTop

  • NSLayoutAttributeBottom

  • NSLayoutAttributeWidth

  • NSLayoutAttributeHeight

  • NSLayoutAttributeBaseline

  • NSLayoutAttributeCenterX

  • NSLayoutAttributeCenterY

  • NSLayoutAttributeLeading

  • NSLayoutAttributeTrailing

Let’s consider a hypothetical constraint. Say you wanted the image view to be 1.5 times as wide as it is tall. You cannot do this with a visual format string, so you would create it individually instead with the following code. (Do not type this hypothetical constraint in your code! It will conflict with others you already have.)

N​S​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​ ​*​a​s​p​e​c​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​ ​c​o​n​s​t​r​a​i​n​t​W​i​t​h​I​t​e​m​:​s​e​l​f​.​i​m​a​g​e​V​i​e​w​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​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​W​i​d​t​h​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​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​E​q​u​a​l​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​o​I​t​e​m​:​s​e​l​f​.​i​m​a​g​e​V​i​e​w​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​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​H​e​i​g​h​t​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​m​u​l​t​i​p​l​i​e​r​:​1​.​5​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​c​o​n​s​t​a​n​t​:​0​.​0​]​;​

To understand how this method works, think of this constraint as the equation shown in Figure 16.4.

Figure 16.4  NSLayoutConstraint equation

NSLayoutConstraint equation

You relate a layout attribute of one view to the layout attribute of another view using a multiplier and a constant to define a single constraint.

To add a single constraint to a view, you use the method

-​ ​(​v​o​i​d​)​a​d​d​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​ ​*​)​c​o​n​s​t​r​a​i​n​t​

The same logic applies to decide which view should receive this message. When using this method, the determination is even easier to make because the affected view objects are the first and fourth arguments. In this case, the only affected object is the image view, so you would add aspectConstraint to that view:

[​s​e​l​f​.​i​m​a​g​e​V​i​e​w​ ​a​d​d​C​o​n​s​t​r​a​i​n​t​:​a​s​p​e​c​t​C​o​n​s​t​r​a​i​n​t​]​;​
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset