Reputation: 680
I haven’t been using AutoLayout constraints for long, so I haven’t fully grasp some of its inner workings.
In particular, are there times I can use the widthAnchor
and the leading
and trailing
anchors interchangeably, and others that I need all 3? Or, is valid to use the widthAnchor
with the leadingAnchor
or the trailingAnchor
?
To sum up my question, is the combination of the leading
and trailing
anchors just defining the width and the X axis position of a view, just as the widthAnchor
with the leadingAnchor
would?
Upvotes: 0
Views: 929
Reputation: 181
Basically, the auto layout needs at least two constraints for views having an intrinsic size and at least four for views without having an intrinsic size. Let's say your view has an intrinsic size(eg. UITextView), it's just enough to constraint the x-axis and y axis. If you wish, you can specify the fixed size with width anchor and height anchor or dynamic size with constraining top, bottom, leading, trailing anchors based on the screen size but this is just optional. If your view doesn't have an intrinsic content size(eg. UIImageView), you need to specify at least one x-axis, one y-axis and width and height anchor or constraint leading, trailing, top and bottom anchors. The point is if you have the leading and trailing anchors, you don't need to specify width anchor and vice versa. Now, you get the idea. If you constraint top and bottom anchor, you shouldn't add height anchor to avoid conflicts.
Upvotes: 1