I have this class in my playground, that has a UIButton
in it.
Everything is fine, until I try adding constraints with a second item (width/height constraints work).
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
is the right item to work with here, right? It is a UIView
. So I don't see the problem.
Item should be added before adding the constraints
self.addSubview(button)
button.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true