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
How to center a label horizontally for all iOS devices in Swift
提问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 us
to 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?
如何修复它以便在所有设备上输入文本?
Constrains
约束
回答by Mehul Sojitra
回答by Kishore Kumar
Click the label
点击标签
Don't set any heights
Now set top space to the container view
Centre horizontal to container view
Now set the height (or) bottom space to the smiley
不要设置任何高度
现在将顶部空间设置为容器视图
以容器视图为中心水平
现在将高度(或)底部空间设置为笑脸
Add this all constraints you will get what you expected.
添加所有约束,您将得到您所期望的。
回答by S. Karthik
回答by Shubham bairagi
just use this code :
只需使用此代码:
yourLabel.translatesAutoresizingMaskIntoConstraints = YES;
CGPoint centerPoint = yourLabel.center;
centerPoint.x = yourImageView.center.x;
yourLabel.center = centerPoint;