Intrinsic Content Size

Intrinsic content size is information that a view has about how big it should be based on what it displays. For example, a label’s intrinsic content size is based on how much text it is displaying. In your case, the image view’s intrinsic content size is the size of the image that you selected.

Auto Layout takes this information into consideration by creating intrinsic content size constraints for each view. Unlike other constraints, these constraints have two priorities: a content hugging priority and a content compression resistance priority.

Content hugging priority

tells Auto Layout how important it is that the view’s size stay close to, or hug, its intrinsic content. A value of 1000 means that the view should never be allowed to grow larger than its intrinsic content size. If the value is less than 1000, then Auto Layout may increase the view’s size when necessary.

Content compression resistance priority

tells Auto Layout how important it is that the view avoid shrinking, or resist compressing, its intrinsic content. A value of 1000 means that the view should never be allowed to be smaller than its intrinsic content size. If the value is less than 1000, then Auto Layout may shrink the view when necessary.

In addition, both priorities have separate horizontal and vertical values so that you can set different priorities for a view’s height and width. This makes a total of four intrinsic content size priority values per view.

You can see and edit these values in Interface Builder. Reopen BNRDetailViewController.xib. Shift-click to select all three text fields in the canvas. Head to the inspector and select the Intrinsic Content Size tab to reveal the size inspector. Find the Content Hugging Priority and Content Compression Resistance Priority sections.

Figure 16.3  Content priorities

Content priorities

First, notice that these values are not 1000 and thus will never conflict with the constraints that you have added so far. This is why the layout will appear incorrectly with smaller-sized images. The value text field’s content hugging vertical property is 250, which is lower than that of the image view (which is 251), so when faced with a small image, Auto Layout chooses to make the text field taller than its intrinsic content size.

It would be better if the image view had a smaller vertical content hugging and commpression resistance priority than the other subviews. Open BNRDetailViewController.m and update viewDidLoad to lower these priorities.

-​ ​(​v​o​i​d​)​v​i​e​w​D​i​d​L​o​a​d​
{​
 ​ ​ ​ ​[​s​u​p​e​r​ ​v​i​e​w​D​i​d​L​o​a​d​]​;​

 ​ ​ ​ ​U​I​I​m​a​g​e​V​i​e​w​ ​*​i​v​ ​=​ ​[​[​U​I​I​m​a​g​e​V​i​e​w​ ​a​l​l​o​c​]​ ​i​n​i​t​W​i​t​h​I​m​a​g​e​:​n​i​l​]​;​

 ​ ​ ​ ​/​/​ ​T​h​e​ ​c​o​n​t​e​n​t​M​o​d​e​ ​o​f​ ​t​h​e​ ​i​m​a​g​e​ ​v​i​e​w​ ​i​n​ ​t​h​e​ ​X​I​B​ ​w​a​s​ ​A​s​p​e​c​t​ ​F​i​t​:​
 ​ ​ ​ ​i​v​.​c​o​n​t​e​n​t​M​o​d​e​ ​=​ ​U​I​V​i​e​w​C​o​n​t​e​n​t​M​o​d​e​S​c​a​l​e​A​s​p​e​c​t​F​i​t​;​

 ​ ​ ​ ​/​/​ ​D​o​ ​n​o​t​ ​p​r​o​d​u​c​e​ ​a​ ​t​r​a​n​s​l​a​t​e​d​ ​c​o​n​s​t​r​a​i​n​t​ ​f​o​r​ ​t​h​i​s​ ​v​i​e​w​
 ​ ​ ​ ​i​v​.​t​r​a​n​s​l​a​t​e​s​A​u​t​o​r​e​s​i​z​i​n​g​M​a​s​k​I​n​t​o​C​o​n​s​t​r​a​i​n​t​s​ ​=​ ​N​O​;​

 ​ ​ ​ ​/​/​ ​T​h​e​ ​i​m​a​g​e​ ​v​i​e​w​ ​w​a​s​ ​a​ ​s​u​b​v​i​e​w​ ​o​f​ ​t​h​e​ ​v​i​e​w​
 ​ ​ ​ ​[​s​e​l​f​.​v​i​e​w​ ​a​d​d​S​u​b​v​i​e​w​:​i​v​]​;​

 ​ ​ ​ ​/​/​ ​T​h​e​ ​i​m​a​g​e​ ​v​i​e​w​ ​w​a​s​ ​p​o​i​n​t​e​d​ ​t​o​ ​b​y​ ​t​h​e​ ​i​m​a​g​e​V​i​e​w​ ​p​r​o​p​e​r​t​y​
 ​ ​ ​ ​s​e​l​f​.​i​m​a​g​e​V​i​e​w​ ​=​ ​i​v​;​

 ​ ​ ​ ​/​/​ ​S​e​t​ ​t​h​e​ ​v​e​r​t​i​c​a​l​ ​p​r​i​o​r​i​t​i​e​s​ ​t​o​ ​b​e​ ​l​e​s​s​ ​t​h​a​n​
 ​ ​ ​ ​/​/​ ​t​h​o​s​e​ ​o​f​ ​t​h​e​ ​o​t​h​e​r​ ​s​u​b​v​i​e​w​s​
 ​ ​ ​ ​[​s​e​l​f​.​i​m​a​g​e​V​i​e​w​ ​s​e​t​C​o​n​t​e​n​t​H​u​g​g​i​n​g​P​r​i​o​r​i​t​y​:​2​0​0​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​A​x​i​s​:​U​I​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​A​x​i​s​V​e​r​t​i​c​a​l​]​;​
 ​ ​ ​ ​[​s​e​l​f​.​i​m​a​g​e​V​i​e​w​ ​s​e​t​C​o​n​t​e​n​t​C​o​m​p​r​e​s​s​i​o​n​R​e​s​i​s​t​a​n​c​e​P​r​i​o​r​i​t​y​:​7​0​0​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​f​o​r​A​x​i​s​:​U​I​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​A​x​i​s​V​e​r​t​i​c​a​l​]​;​

 ​ ​ ​ ​.​.​.​
}​

Build and run again. Now, when dealing with smaller-sized images, Auto Layout will change the image size and leave the height of the text fields alone.

..................Content has been hidden....................

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