在Xcode中纵向和横向居中多个标签

时间:2017-08-23 03:46:01

标签: ios xcode autolayout

我是IOS的新编程,我想创建一个这样的界面:

enter image description here

但是我无法垂直和水平居中那五个标有红色方块的标签,因为它们相互叠加。

所以,我的问题是如何将一组标签垂直和水平居中,以获得像上一张图​​像一样的界面。

此时我的界面是这样的:

enter image description here

3 个答案:

答案 0 :(得分:5)

使用堆栈视图按照以下步骤进行操作

1)根据需要添加5个标签,并根据需要添加约束

enter image description here

2)现在选择您使用约束创建的所有标签,并在堆栈视图中按下故障板中右侧第二个选项的堆栈选项

enter image description here

3)现在您将注意到为标签提供的所有约束都是nil并且嵌入在stackView中现在需要为stackView提供约束。在容器中水平和垂直选择

enter image description here

4)这里是输出

enter image description here

答案 1 :(得分:0)

如果您想将标签置于堆栈中心,

选择堆栈并将其对齐属性从“前导”更改为“中心”。

如果您希望标签堆栈位于屏幕的中心, 将autolayout中的对齐方式设置为容器中的水平方向 并垂直放入容器中。

答案 2 :(得分:0)

堆栈可能是最佳选择。

Add some labels Select the labels Click the "Stack" button Labels are now in a stack Center the stack horizontally and vertically

相关问题