0
votes

I want to make a child view stretch to its parent view full size. What is the correct way to use auto layout?

  1. Pin all edges to parent view (leading, top, trailing, bottom edges)
  2. Set equal-width + center-X + equal-height + center-Y
2
I prefer using first option, leading, trailing,top and bottom equalToSuperView - Ronaldo Albertini
The only reasons you'd chose one over the other is (a) consistency with how you code it throughout the app and (b) if view changes may take place and you need a view to shrink in a certain direction (i.e. from top to bottom whereby you'd need a bottom anchor). The third reason is personal preference. - liquid

2 Answers

1
votes

Both the options you suggest will do the job ( first 1 is the common ) in addition

1- top ,leading ,equal width and height

2- bottom ,trailing,equal width and height

3- top ,leading ,trailing and equal height

4- top , bottom ,leading,equal width

also there are many ways to do this , there is no set correct over the other the most important thing is having non-breaking constraints as if conflicts occured there will be performance problems in addition to unexpected UI results

0
votes

As I see it, option 1 is the only way to go. Both options will do the trick, but I strongly believe that 1 is way more "correct" than option 2.

Auto layout is a language, and as such we should use its grammar correctly. As in language, there are multiple ways to deliver a message, but some are better than others.

In this case, option 1 describes exactly what we want to do, and describes it literally: pin all of the child edges to its parent edges. Option 2, is an abuse of an edge case of centering a view, where its size is the same as another view. If we would instruct verbally our code, this is not what we would say.