Basically, you need to limit four things:
- the leading space of your subtask for its supervisor should be zero
- the top space of your subquery must be zero for its supervisor
- the width of your subtask will be equal to the width of the supervisor
- the height of your subtask will be equal to the width of the supervisor
If a visual constraint does not work for you, you can build these four constraints individually in code. Use the +constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier: constant: method to specify exact relationships between attributes of different views. For example, restriction No. 1 above can be expressed:
[NSLayoutConstraint constraintWithItem:mySubview attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:mySuperview attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]
and # 3 can be:
[NSLayoutConstraint constraintWithItem:mySubview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:mySuperview attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]
Once you create these four constraints, you can add them to your supervisor as needed.
Note that there are several ways to achieve the same effect as above:
- You can limit the end space and bottom space instead of width and height
- You can limit the center of X and the center of Y instead of the top and top spaces
You may also encounter the same limitations in visual presentation as in Peter Hawesy's answer. For example, an equal-width constraint might look like @"[mySubview(==mySuperview)]" with the corresponding view dictionary.
Keep in mind that the Auto Layout Guide provides extensive information on limitations, including how to debug them when something goes wrong.
Tim
source share