在 UIStackView 中水平居中不同宽度的视图

Jas*_*man 6 uikit uiview ios uistackview

我想设置一个 UIViewStack 以便它将两个视图居中放置,即使它们具有不同的宽度。下面是一个例子:

例子

是否可以使用 UIStackView 实现这种类型的配置?我似乎无法弄清楚!

任何帮助,将不胜感激。

iMu*_*hid 5

您应该使用嵌套的StackView. 首先将 View1 和 View2 嵌入到Horizontal StackView. 按比例设置对齐属性中心和分布填充。然后嵌入Horizontal StackViewVertical Stackview。在这里,我附上了我的演示屏幕截图:在此处输入图片说明


den*_*pro 2

不,你不能。来自苹果的文档

\n\n
\n

堆栈视图使用自动布局来定位其排列视图并调整其大小。stack 视图将第一个和最后一个排列的视图与其边缘沿 stack\xe2\x80\x99s 轴对齐。In a horizontal stack, this means the first arranged view\xe2\x80\x99s leading edge is pinned to the stack\xe2\x80\x99s leading edge, and the last arranged view\xe2\x80\x99s trailing edge is pinned to the stack\xe2\x80\x99s trailing edge.

\n
\n\n

您可以使用约束来代替。

\n