You can add it to your constraints like this:
NSLayoutConstraint.activate([
theImg.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 280),
theImg.centerXAnchor.constraint(equalTo: view.centerXAnchor),
theImg.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
theImg.widthAnchor.constraint(equalToConstant: 40),
theImg.heightAnchor.constraint(equalToConstant: 40)
])
NSLayoutConstraint.activate
is a better approach to activate/deactivate constraints, because you can use an array of constraints and deactivate/activate them, instead of setting isActive
one by one.
Also, unless you want your left/leading constraint to be on the left, even on right to left languages, you should use leadingAnchor
instead of leftAnchor
.
More info
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…