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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34448619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:21:03  来源:igfitidea点击:

How to center a label horizontally for all iOS devices in Swift

iosxcodeswiftswift2ios9

提问by Katz

I can't figure out how to make the label horizontally center in a image view, The label says "You've been here. What would you rate us, I want the What would you rate usto be under the "You've been here."What I attempted to do accomplish this was to add 2 lines to the labels and select the label plus the 3 buttons and clicked on "Add Missing Constrains". When I run the program this is the result.

我无法弄清楚如何使在图像视图标签水平居中,标签上写着"You've been here. What would you rate us,我想What would you rate us成为下"You've been here."我试图达到的目的,这是2行添加到标签,并选择标签加3 个按钮,然后单击“添加缺少的约束”。当我运行程序时,这是结果。

How can I fix it so the text is entered on all devices?

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

enter image description here

在此处输入图片说明

Constrains

约束

enter image description here

在此处输入图片说明

回答by Mehul Sojitra

for your label try to add constraints like:

为您的标签尝试添加约束,如:

Add top, width and height constraints as shown in below screen shot and make sure that constraints to margins is uncheck.

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

enter image description here

在此处输入图片说明

And set horizontal center constraints.

并设置水平中心约束。

enter image description here

在此处输入图片说明

回答by Kishore Kumar

Click the label

点击标签

  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

  1. 不要设置任何高度

  2. 现在将顶部空间设置为容器视图

  3. 以容器视图为中心水平

  4. 现在将高度(或)底部空间设置为笑脸

Add this all constraints you will get what you expected.

添加所有约束,您将得到您所期望的。

回答by S. Karthik

enter image description here

在此处输入图片说明

  1. Add Vertical top spacing for the particular imageview
  2. Add center horizontal at in same imageview
  3. Then adjust the leading and trailing as same as drag and drop way. it just easy.
  1. 为特定图像视图添加垂直顶部间距
  2. 在同一图像视图中添加水平中心
  3. 然后像拖放方式一样调整前导和尾随。这很容易。

回答by Shubham bairagi

just use this code :

只需使用此代码:

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