如何在Swift中为所有iOS设备水平居中标签

时间:2015-12-24 06:57:41

标签: ios xcode swift swift2 ios9

我无法弄清楚如何在图像视图中使标签水平居中,标签显示为What would you rate us,我希望"You've been here."位于File file = new File("C:\\Example"); String[] myFiles; if (file.isDirectory()) { myFiles = file.list(); for (int i = 0; i < myFiles.length; i++) { File myFile = new File(file, myFiles[i]); myFile.delete(); } } 我尝试的内容之下要做到这一点,就是在标签上添加2行,选择标签加上3个按钮,然后单击“添加缺失约束”。当我运行程序时,这就是结果。

如何修复它以便在所有设备上输入文字?

enter image description here

约束

enter image description here

4 个答案:

答案 0 :(得分:14)

为您的标签尝试添加以下约束:

添加顶部,宽度和高度约束,如下面的屏幕截图所示,并确保取消选中边距约束。

enter image description here

设置水平中心约束。

enter image description here

答案 1 :(得分:0)

点击标签

1.Don't set any heights 

2.Now set top space to the container view

3.centre horizontal to container view

4.now set the height (or) bottom space to the smiley 

Add this all constraints you will get what you expected :)

答案 2 :(得分:0)

enter image description here

  1. 为特定图像视图添加垂直顶部间距
  2. 在相同的imageview中添加水平中心
  3. 然后调整前导和尾随与拖放方式相同。这很简单。

答案 3 :(得分:0)

只需使用此代码:

yourLabel.translatesAutoresizingMaskIntoConstraints = YES;
CGPoint centerPoint = yourLabel.center;
centerPoint.x = yourImageView.center.x;
yourLabel.center = centerPoint;