I have code that programmatically creates automatic layout constraints and adds them to the view.
There are two ways to do this: call addConstraints in addConstraints or set .isActive = true for each constraint (which internally calls addConstraint)
Option 1:
parent.addConstraints([ child.topAnchor.constraint(equalTo: parent.topAnchor, constant: 20), child.leftAnchor.constraint(equalTo: parent.leftAnchor, constant: 5) ])
Option 2:
child.topAnchor.constraint(equalTo: parent.topAnchor, constant: 20).isActive = true child.leftAnchor.constraint(equalTo: parent.leftAnchor, constant: 5).isActive = true
My question is, is there any benefit to doing one on top of the other? (performance / etc.), or it happens purely to style.
(I donβt think that the restrictions are evaluated until the next layout passes, so I donβt think it is important that we add them one by one, and not in the block?)
If it's just a style, which style is more preferable for the community?
(I personally prefer addConstraints, however it is very close, and I can easily shake .ISActive)
source share