在 XCode 游乐场中添加约束时出错
Error when adding constraints in XCode Playground
我的操场上有这个 class,里面有一个 UIButton
。
一切都很好,直到我尝试用第二个项目添加约束(width/height 约束工作)。
class ButtonView: UIView {
required init?(coder aDecoder: NSCoder) { ... }
override init(frame: CGRect) {
super.init(frame: frame)
//ButtonView Setup
...
let button = Button()
//Button Constraints
button.translatesAutoresizingMaskIntoConstraints = false
button.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true //Error here
self.layoutIfNeeded()
self.addSubview(button)
}
var button: Button?
}
self
是在这里工作的正确项目,对吧?这是一个UIView
。所以我没有看到问题。
应在添加约束之前添加项目
self.addSubview(button)
button.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
我的操场上有这个 class,里面有一个 UIButton
。
一切都很好,直到我尝试用第二个项目添加约束(width/height 约束工作)。
class ButtonView: UIView {
required init?(coder aDecoder: NSCoder) { ... }
override init(frame: CGRect) {
super.init(frame: frame)
//ButtonView Setup
...
let button = Button()
//Button Constraints
button.translatesAutoresizingMaskIntoConstraints = false
button.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true //Error here
self.layoutIfNeeded()
self.addSubview(button)
}
var button: Button?
}
self
是在这里工作的正确项目,对吧?这是一个UIView
。所以我没有看到问题。
应在添加约束之前添加项目
self.addSubview(button)
button.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true