iOS - Better Way Of Setting The Constraint Of Two Subviews

喜夏-厌秋 提交于 2019-12-12 00:35:33

问题


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.

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

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!


回答1:


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.




回答2:


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.

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



来源:https://stackoverflow.com/questions/36659797/ios-better-way-of-setting-the-constraint-of-two-subviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!