问题描述
自动布局让我的生活变得困难.理论上,当我切换时它会非常有用,但我似乎一直在努力.
Auto Layout is making my life difficult. In theory, it was going to be really useful when I switched, but I seem to fight it all of the time.
我制作了一个演示项目来尝试寻求帮助.有谁知道如何在调整视图大小时使视图之间的空间均匀增加或减少?
I've made a demo project to try to find help. Does anyone know how to make the spaces between views increase or decrease evenly whenever the view is resized?
这里是三个标签(手动垂直间隔):
Here are three labels (manually spaced vertically even):
我想要的是让他们在我旋转时均匀地调整间距(而不是视图大小).默认情况下,顶部和底部视图向中心挤压:
What I want is for them to resize their spacing (not the view size) evenly when I rotate. By default, the top and bottom views squish towards the center:
推荐答案
所以我的方法允许您在界面生成器中执行此操作.您所做的是创建您已设置为同等匹配高度的间隔视图".然后将顶部和底部约束添加到标签(参见屏幕截图).
So my approach allows you to do this in interface builder. What you do is create 'spacer views' that you have set to match heights equally. Then add top and bottom constraints to the labels (see the screenshot).
更具体地说,我对Spacer View 1"有一个最高限制,以 superview 的高度约束低于 1000 的优先级并且高度等于所有其他spacer 视图".Spacer View 4"对超级视图有底部空间限制.每个标签对其最近的间隔视图"都有各自的顶部和底部约束.
More specifically, I have a top constraint on 'Spacer View 1' to superview with a height constraint of lower priority than 1000 and with Height Equals to all of the other 'spacer views'. 'Spacer View 4' has a bottom space constraint to superview. Each label has a respective top and bottom constraints to its nearest 'spacer views'.
注意:请确保您的标签上没有额外的顶部/底部空间限制以进行超级视图;只是空间视图"的那些.这将是可以满足的,因为顶部和底部约束分别在Space View 1"和Spacer View 4"上.
Note: Be sure you DON'T have extra top/bottom space constraints on your labels to superview; just the ones to the 'space views'. This will be satisfiable since the top and bottom constraints are on 'Space View 1' and 'Spacer View 4' respectively.
Duh 1:我复制了我的视图,只是将其置于横向模式,以便您可以看到它有效.
Duh 1: I duplicated my view and merely put it in landscape mode so you could see that it worked.
Duh 2:间隔视图"可能是透明的.
Duh 2: The 'spacer views' could have been transparent.
Duh 3:这种方法可以横向应用.
Duh 3: This approach could be applied horizontally.
这篇关于在容器视图中均匀分布多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!