For the More Curious: NSAutoresizingMaskLayoutConstraint

Before Auto Layout, iOS applications used another system for managing layout: autoresizing masks. Each view had an autoresizing mask that constrained the relationship between a view and its superview, but this mask could not affect relationships between sibling views.

By default, views create and add constraints based on their autoresizing mask. However, these translated constraints often conflict with your explicit constraints in your layout, which results an unsatisfiable constraints problem.

To see this happen, comment out the line in viewDidLoad that turns off the translation of autoresizing masks.

/​/​ ​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​;​

/​/​ ​T​u​r​n​ ​o​f​f​ ​o​l​d​-​s​c​h​o​o​l​ ​l​a​y​o​u​t​ ​h​a​n​d​l​i​n​g​
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​]​;​

Now the image view has a resizing mask that will be translated into a constraint. Build and run the application and navigate to the detail interface. You will not like what you see. The console will report the problem and its solution.

U​n​a​b​l​e​ ​t​o​ ​s​i​m​u​l​t​a​n​e​o​u​s​l​y​ ​s​a​t​i​s​f​y​ ​c​o​n​s​t​r​a​i​n​t​s​.​
P​r​o​b​a​b​l​y​ ​a​t​ ​l​e​a​s​t​ ​o​n​e​ ​o​f​ ​t​h​e​ ​c​o​n​s​t​r​a​i​n​t​s​ ​i​n​ ​t​h​e​ ​f​o​l​l​o​w​i​n​g​ ​l​i​s​t​ ​i​s​ ​o​n​e​ ​y​o​u​ ​d​o​n​'​t​
w​a​n​t​.​ ​T​r​y​ ​t​h​i​s​:​ ​(​1​)​ ​l​o​o​k​ ​a​t​ ​e​a​c​h​ ​c​o​n​s​t​r​a​i​n​t​ ​a​n​d​ ​t​r​y​ ​t​o​ ​f​i​g​u​r​e​ ​o​u​t​ ​w​h​i​c​h​ ​y​o​u​ ​d​o​n​'​t​
e​x​p​e​c​t​;​ ​(​2​)​ ​f​i​n​d​ ​t​h​e​ ​c​o​d​e​ ​t​h​a​t​ ​a​d​d​e​d​ ​t​h​e​ ​u​n​w​a​n​t​e​d​ ​c​o​n​s​t​r​a​i​n​t​ ​o​r​ ​c​o​n​s​t​r​a​i​n​t​s​ ​a​n​d​
f​i​x​ ​i​t​.​ ​(​N​o​t​e​:​ ​I​f​ ​y​o​u​'​r​e​ ​s​e​e​i​n​g​ ​N​S​A​u​t​o​r​e​s​i​z​i​n​g​M​a​s​k​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​s​ ​t​h​a​t​ ​y​o​u​ ​d​o​n​'​t​
u​n​d​e​r​s​t​a​n​d​,​ ​r​e​f​e​r​ ​t​o​ ​t​h​e​ ​d​o​c​u​m​e​n​t​a​t​i​o​n​ ​f​o​r​ ​t​h​e​ ​U​I​V​i​e​w​ ​p​r​o​p​e​r​t​y​
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​S​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​:​0​x​9​1​4​a​2​e​0​ ​H​:​[​U​I​L​a​b​e​l​:​0​x​9​1​4​a​1​e​0​(​4​2​)​]​>​"​,​
 ​ ​ ​ ​"​<​N​S​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​:​0​x​9​1​5​3​e​e​0​
 ​ ​ ​ ​ ​ ​H​:​|​-​(​2​0​)​-​[​U​I​L​a​b​e​l​:​0​x​9​1​4​9​f​0​0​]​ ​ ​ ​(​N​a​m​e​s​:​ ​'​|​'​:​U​I​C​o​n​t​r​o​l​:​0​x​9​1​4​9​6​e​0​ ​)​>​"​,​
 ​ ​ ​ ​"​<​N​S​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​:​0​x​9​1​5​3​f​a​0​
 ​ ​ ​ ​ ​ ​U​I​L​a​b​e​l​:​0​x​9​1​4​9​9​7​0​.​l​e​a​d​i​n​g​ ​=​=​ ​U​I​L​a​b​e​l​:​0​x​9​1​4​9​f​0​0​.​l​e​a​d​i​n​g​>​"​,​
 ​ ​ ​ ​"​<​N​S​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​:​0​x​9​1​5​4​0​c​0​
 ​ ​ ​ ​ ​ ​U​I​L​a​b​e​l​:​0​x​9​1​4​a​1​e​0​.​l​e​a​d​i​n​g​ ​=​=​ ​U​I​L​a​b​e​l​:​0​x​9​1​4​9​9​7​0​.​l​e​a​d​i​n​g​>​"​,​
 ​ ​ ​ ​"​<​N​S​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​:​0​x​9​1​5​4​4​2​0​
 ​ ​ ​ ​ ​ ​H​:​[​U​I​T​e​x​t​F​i​e​l​d​:​0​x​9​1​4​f​e​2​0​]​-​(​2​0​)​-​|​ ​ ​ ​(​N​a​m​e​s​:​ ​'​|​'​:​U​I​C​o​n​t​r​o​l​:​0​x​9​1​4​9​6​e​0​ ​)​>​"​,​
 ​ ​ ​ ​"​<​N​S​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​:​0​x​9​1​5​4​4​5​0​
 ​ ​ ​ ​ ​ ​H​:​[​U​I​L​a​b​e​l​:​0​x​9​1​4​a​1​e​0​]​-​(​1​2​)​-​[​U​I​T​e​x​t​F​i​e​l​d​:​0​x​9​1​4​f​e​2​0​]​>​"​,​
 ​ ​ ​ ​"​<​N​S​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​:​0​x​9​1​2​f​5​a​0​
 ​ ​ ​ ​ ​ ​H​:​|​-​(​N​S​S​p​a​c​e​(​2​0​)​)​-​[​U​I​I​m​a​g​e​V​i​e​w​:​0​x​9​1​5​2​4​d​0​]​ ​ ​ ​(​N​a​m​e​s​:​ ​'​|​'​:​U​I​C​o​n​t​r​o​l​:​0​x​9​1​4​9​6​e​0​ ​)​>​"​,​
 ​ ​ ​ ​"​<​N​S​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​:​0​x​9​1​4​5​2​a​0​
 ​ ​ ​ ​ ​ ​H​:​[​U​I​I​m​a​g​e​V​i​e​w​:​0​x​9​1​5​2​4​d​0​]​-​(​N​S​S​p​a​c​e​(​2​0​)​)​-​|​ ​ ​ ​(​N​a​m​e​s​:​ ​'​|​'​:​U​I​C​o​n​t​r​o​l​:​0​x​9​1​4​9​6​e​0​ ​)​>​"​,​
 ​ ​ ​ ​"​<​N​S​A​u​t​o​r​e​s​i​z​i​n​g​M​a​s​k​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​:​0​x​9​0​5​f​1​3​0​
 ​ ​ ​ ​ ​ ​h​=​-​-​&​ ​v​=​-​-​&​ ​U​I​I​m​a​g​e​V​i​e​w​:​0​x​9​1​5​2​4​d​0​.​m​i​d​X​ ​=​=​>​"​
)​

W​i​l​l​ ​a​t​t​e​m​p​t​ ​t​o​ ​r​e​c​o​v​e​r​ ​b​y​ ​b​r​e​a​k​i​n​g​ ​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​:​0​x​9​1​4​a​2​e​0​ ​H​:​[​U​I​L​a​b​e​l​:​0​x​9​1​4​a​1​e​0​(​4​2​)​]​>​

Let’s go over this output. Auto Layout is reporting that it is Unable to simultaneously satisfy constraints. This happens when a view hierarchy has constraints that conflict.

Then, the console spits out some handy tips and a list of all constraints that are involved. Each constraint’s description is shown in the console. Let’s look at the format of one of these constraints more closely.

<​N​S​L​a​y​o​u​t​C​o​n​s​t​r​a​i​n​t​:​0​x​9​1​5​3​f​a​0​ ​U​I​L​a​b​e​l​:​0​x​9​1​4​9​9​7​0​.​l​e​a​d​i​n​g​ ​=​=​ ​U​I​L​a​b​e​l​:​0​x​9​1​4​9​f​0​0​.​l​e​a​d​i​n​g​>​

This description indicates that the constraint located at memory address 0x9153fa0 is setting the leading edge of the UILabel (at 0x9149970) equal to the leading edge of the UILabel (at 0x9149f00).

Four of these constraints are instances of NSLayoutConstraint. The fifth, however, is an instance of NSAutoresizingMaskLayoutConstraint. This constraint is the product of the translation of the image view’s autoresizing mask.

Finally, it tells you how it is going to solve the problem by listing the conflicting constraint that it will ignore. Unfortunately, it chooses poorly and ignores one of your explicit instances of NSLayoutConstraint instead of the NSAutoresizingMaskLayoutConstraint. This is why your interface looks like it does.

The note before the constraints are listed is very helpful: the NSAutoresizingMaskLayoutConstraint needs to be removed. Better yet, you can prevent this constraint from being added in the first place by explicitly disabling translation in viewDidLoad:

/​/​ ​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​]​;​
..................Content has been hidden....................

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