如何在 Interface Builder 中设置自动布局以垂直排列视图,如图所示:
How to set up autolayout in Interface Builder to arrange views vertically as shown on image:
Top view
固定在屏幕顶部边缘,Bottom view
固定在屏幕底部边缘.My view
应该在顶视图和底视图之间居中(因此到 Top view
的距离等于到 Bottom view
的距离)
Top view
pinned to top screen edge, Bottom view
pinned to bottom screen edge. My view
should be centered between top and bottom views (so distance to Top view
equals to distance to Bottom view
)
这样做的方法是在可见视图之间放置 2 个不可见的间隔"视图.
The way to do this is to have 2 invisible "spacer" views between you visible views.
您不能使空间具有相同的高度.但是您使用间隔"视图并将它们放置在您的视图之间(与周围视图之间的间隙为 0).
You can't make spaces have equal height. But you use the "spacer" views and place them between your views (with 0 gap to the surrounding views).
然后你给这些视图相等的高度,它们会将你的视图围绕在 My View
的中心,与 Bottom View
和 Top View代码>.
Then you give these views equal heights and they will push your views around to centre the My View
with equal gap to the Bottom View
and Top View
.
即像这样……
V:|[Top View][spacer1][My View][spacer2(==spacer1)][Bottom View]|
编辑 - 另一种方式
我只是想到了另一种方法.您可以在 Top View
和 Bottom View
之间有一个不可见的容器 UIView
,没有间隙.
I just thought of another way of doing this. You could have an invisible container UIView
that is between Top View
and Bottom View
with no gap.
然后你将 My View
放在这个不可见的视图中并垂直居中.
Then you place My View
inside this invisible view and centre it vertically.
这篇关于iOS自动布局使我的视图在两个视图之间居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!