by melling · 6y ago · 0 comments
Col.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
Col.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
Col.leftAnchor.constraint(equalTo: leftAnchor, constant: 0).isActive = true
Col.rightAnchor.constraint(equalTo: rightAnchor, constant: 0).isActive = true
I prefer:NSLayoutConstraint.activate([
Col.topAnchor.constraint(equalTo: topAnchor, constant: 0),
Col.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0),
Col.leftAnchor.constraint(equalTo: leftAnchor, constant: 0),
Col.rightAnchor.constraint(equalTo: rightAnchor, constant: 0),
])GitHub example with better readability:
https://github.com/melling/ios_topics/tree/master/ButtonCent...
The rule that I followed was:
Use activateConstraints if you actually have a performance problem and otherwise activate constraints one by one.
However since this is a framework this rule may not apply because you don't know how it will be used.
All it takes is forgetting to add `.isActive = true` to a single constraint and nothing will work, and it can be so difficult to spot.