ios 添加子视图并以编程方式使用约束对其进行定位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26721920/
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
Adding a subview and position it with constraints programmatically
提问by Isuru
I have added a UIView
using the IB on to the view controller. Let's call it the redView
.
我已经UIView
使用 IB 在视图控制器上添加了一个。我们称之为redView
.
Then I have pinned its all four sides using auto layout constraints in code and when I run it, it looks like this as expected.
然后我在代码中使用自动布局约束固定了它的所有四个边,当我运行它时,它看起来像预期的那样。
Now I want to add a UILabel
to this view programmatically and position it in the center using auto layout constraints.
现在我想以UILabel
编程方式向这个视图添加一个并使用自动布局约束将它放置在中心。
Below is the code I have so far.
下面是我到目前为止的代码。
import UIKit
class ViewController: UIViewController {
@IBOutlet private var redView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
redView.setTranslatesAutoresizingMaskIntoConstraints(false)
let leadingConstraint = NSLayoutConstraint(item: redView, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: redView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1, constant: 0)
let topConstraint = NSLayoutConstraint(item: redView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: redView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1, constant: 0)
view.addConstraints([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint])
let label = UILabel()
label.text = "Auto Layout Exercise"
redView.addSubview(label)
let xCenterConstraint = NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: redView, attribute: .CenterX, multiplier: 1, constant: 0)
let yCenterConstraint = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: redView, attribute: .CenterY, multiplier: 1, constant: 0)
let leadingConstraint1 = NSLayoutConstraint(item: label, attribute: .Leading, relatedBy: .Equal, toItem: redView, attribute: .Leading, multiplier: 1, constant: 20)
let trailingConstraint1 = NSLayoutConstraint(item: label, attribute: .Trailing, relatedBy: .Equal, toItem: redView, attribute: .Trailing, multiplier: 1, constant: -20)
redView.addConstraints([xCenterConstraint, yCenterConstraint, leadingConstraint1, trailingConstraint1])
}
}
The problem is the label doesn't appear on the redView
. Can anyone please tell me what I'm missing here?
问题是标签没有出现在redView
. 谁能告诉我我在这里缺少什么?
Thank you.
谢谢你。
回答by Glynbeard
I'm pretty sure you need to also set:
我很确定您还需要设置:
label.setTranslatesAutoresizingMaskIntoConstraints(false)
Otherwise I don't think the constraints will apply to the label.
否则我认为这些限制不适用于标签。
回答by Nikita Kurtin
Note for Swift 2/3
Swift 2/3 的注意事项
It has changed to Boolean property: UIView.translatesAutoresizingMaskIntoConstraints
它已更改为布尔属性:UIView.translatesAutoresizingMaskIntoConstraints
Instead of it previously being:
而不是以前的:
label.setTranslatesAutoresizingMaskIntoConstraints(false)
It is now:
就是现在:
label.translatesAutoresizingMaskIntoConstraints = false
回答by jaiswal Rajan
You need to do this for each UIControl to assign NSConstraints
您需要为每个 UIControl 执行此操作以分配 NSConstraints
label.translatesAutoresizingMaskIntoConstraints = false