This workshop will be retired on May 31, 2020.
Constraint Priorities3:45 with Pasan Premaratne
In addition to defining constraints we can tell Auto Layout which ones are more important than others through the use of priority values
To help auto-layout resolve the issue at hand, 0:00 we're going to look at concept number two in our toolbox, constraint priorities. 0:02 The length of constraint, any constraint should do. 0:07 But I'm going to go back to the iPhone 8 layout where we can see the second label, 0:10 and select our greater than or equal constraint. 0:15 When you double click it on the size inspector or go directly to the size 0:18 inspector by selecting it in the layout, you can see the constraints attributes. 0:21 Right below the constant value, you should see a field for a priority setting. 0:27 By default, all constraints are required. 0:32 When we define constraints, 0:35 auto-layout needs to figure out a layout that satisfies all our constraints. 0:36 We know this, 0:41 because we're in a situation where auto-layout cannot calculate a solution. 0:42 What we need to do is make one of our constraints optional, so 0:46 that in a situation where auto-layout cannot determine a solution, 0:50 it can try to arrive at one by not installing the optional constraint. 0:54 So how do we make a constraint optional? 0:59 All constraints define a priority value as a number between 1 and 1000. 1:02 A priority value of 1000 means the constraint is required. 1:07 Any priority value below 1000 indicates that this constraint is optional. 1:13 When auto-layout determines a layout or attempts to determine a layout, 1:18 it attempts to satisfy all constraints in priority order from highest to lowest. 1:22 If an optional constraint cannot be satisfied, the system skips it and 1:27 continues to the next constraint. 1:32 Now this doesn't necessarily mean that the optional constraint is ignored altogether. 1:35 If after skipping that constraint and arriving at a solution, 1:39 there's some ambiguity in the layout, auto-layout will then proceed to select 1:43 a solution that comes as close as possible to satisfying the optional constraint. 1:47 So let's go ahead and modify the priority value of the bottom space constraint here. 1:52 So let's select the bottom space constraint on the second label, and 1:57 we'll set a Priority value to 750. 2:01 I mentioned earlier that constraint values can range from 1 to 1,000. 2:05 But in normal usage, you shouldn't be picking any random values. 2:09 So auto-layout views priority values of 1,000 as required. 2:14 And when you click this drop down, you can see that 750 is considered a high value. 2:17 500, which is not mentioned here, is medium, and 250 is low. 2:22 By setting this constraint to a value of 750, 2:28 we're indicating that this constraint is optional because it's below 1000. 2:31 But it is still pretty important, it's high, so 2:35 we can use these different values to differentiate between the two. 2:38 When you do this, let's go to the last device or the second last, and 2:42 then the last. 2:46 And now you can see that we have blue I bars all around, and 2:47 our constraints seem to work. 2:51 This occurs because we've told auto-layout that 2:53 when there are unsatisfiable constraints, you cannot resolve the layout. 2:56 It should then prioritized the vertical space constraint between the first and 3:01 the second label. 3:04 And set it 32 points away, rather than setting it to 200 3:06 points from the bottom as the bottom vertical space constraints specifies. 3:10 Okay, so any quality relations and priorities are closely related. 3:15 And as we just did, you'll find yourself using the two concepts together often. 3:20 Now, we need to get some practice doing this using these concepts 3:25 before we move on. 3:29 So in the note section of this video, 3:30 you should find a link to some additional resources. 3:31 Before you proceed, as always, check out the cookbook videos to get more practice 3:35 in with inequality relations and constraint priorities. 3:39 And then when you're ready, come back here. 3:43
You need to sign up for Treehouse in order to download course files.Sign up