在容器视图中均匀间隔多个视图 [英] Evenly space multiple views within a container view

查看:27
本文介绍了在容器视图中均匀间隔多个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自动布局让我的生活变得困难.理论上,当我切换时它会非常有用,但我似乎一直在与它作斗争.

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' 有一个最高限制,它的高度限制低于 1000,并且高度等于所有其他间隔视图".'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.

这篇关于在容器视图中均匀间隔多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆