iOS auto layout: how to dynamically evenly space items?

六月ゝ 毕业季﹏ 提交于 2020-01-02 05:40:07

问题


I need to evenly space X items, and the container for the items can have dynamic width, and I want the first and last items to stick to left and right edge:

|-0-[item1]-[...]-[itemX]-0-|

so that no matter how wide the container is, the items are always evenly spaced, how to implement this with layout constraints?

edit: I was thinking that if I can set item1 and item2 to have same spacing as item2 and item3 and so on, then this should be easy, but I don't think I can set it without a constant width?


回答1:


You can't set spaces to have the same width but you can use "spacer" views.

Put an invisible view between all your views and you can set the format like this...

@"|[view1][spacer1][view2(==view1)][spacer2(==spacer1)]...[spacerN(==spacer1)][viewN]|"

This will make all the views have equal spaces between them.

Make sure to set the spacer views as alpha 0 or hidden or background color clear.



来源:https://stackoverflow.com/questions/19063877/ios-auto-layout-how-to-dynamically-evenly-space-items

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