具有相同宽度和设置优先级的Autolayout

时间:2015-11-18 08:33:49

标签: ios swift autolayout

嗨我需要根据某些条件显示3个或4个按钮,因为我正在使用以下约束(下图)。 enter image description here

当我有4个视图时它工作正常,但是对于3个按钮,第一个按钮的大小不一样,但其余的按钮对齐的宽度相等(下图)。我已经为水平间距约束设置了优先级990,因此其余工作正常。 enter image description here

我在故事板中设置了所有自动布局约束。

当我删除中间视图时,如何获得相等的宽度?

2 个答案:

答案 0 :(得分:1)

iOS9仅限应用:

这可能是使用新Horizontal Stack View的好时机。只需将distribution设置为fill equally,将alignment设置为fill

对于iOS8支持:

您必须在组件之间将宽度约束设置为equal width,并将horizontal spacing设置为0

使用button2将button1设置为等宽,然后使用button3将button1设置为等于width。这应该足够了。

答案 1 :(得分:0)

创建3个按钮,并将其宽度和高度设置为等于父级(Superview)。 接下来为每个按钮"宽度约束" 设置乘数属性,使用" 1/3"价值那样:

enter image description here

enter image description here