JLT
JLT

Reputation: 3172

iOS - Better Way Of Setting The Constraint Of Two Subviews

I have two subviews in a view controller. I want the two subviews to be centered horizontally inside the main view, one on top of the other and also having the same leading space and trailing space with respect to the main view.

One way I can do is to set the leading space and trailing space of the top subview with respect to the main view and also centering horizontally in the main view. After that, align the leading, trailing and center of the bottom subview with the top subview.

enter image description here

The other way is to set the leading space, trailing space, and center of the two subviews independently with respect to the main view

enter image description here

I don't what is the difference between the two ways. Because for me, the two seems to have exactly the same effect.

Which is better and more recommended?

Thanks!

Upvotes: 0

Views: 107

Answers (2)

Rathna Kumaran
Rathna Kumaran

Reputation: 113

Connect the subview width "Equal width" to superview and also for height . Then in "Size Inspector" --> Equal width constraint --> Edit. multiplier usually have "1". reduce it to 0.9 / 0.8 based on your requirement.

enter image description here

Do the same for hight constraint. Then provide center horizontal and top / bottom layout leading space respectively...

Upvotes: 1

Anshuman
Anshuman

Reputation: 16

Both ways are equally good.Though looks like you are not giving height constraints for subviews, if this is the case then please do following based on your requirement :

Height constraint for both subviews. Or Height constraint for top subview and top / bottom space margin constraints for the other subview.

Upvotes: 0

Related Questions