Thursday, March 17, 2016

Constraint Activation

Daniel Jalkut:

Starting in OS X 10.0 and iOS 8.0, I was intrigued by the announcement that NSLayoutConstraint now supports a property called “active,” which can be used to, you guessed it, activate or deactivate a constraint. I assumed this would be an answer to my prayers: a constraint could now be left installed on a view for safe-keeping, but its impact on layout would be negated by setting it to be “inactive.” I envisioned setting up competing groups of constraints on a view and simply activating or deactiving them en masse when the need arose.

I assumed wrong.


So “active” is not a convenience for easily toggling whether an installed layout constraint has an effect or not, but a convenience for the plumbing of installing and removing them.


Because NSLayoutConstraint supports a mutable “priority” property, you can effectively disable it by setting its priority lower than any other constraints that affect the same view.

1 Comment RSS · Twitter

"I assumed wrong" is a recurring theme with AutoLayout. Making sense of constraints in Interface Builder is still a complete mess years after its introduction. Trying to intuit what is going to happen with constraints is just as much of an issue.

Leave a Comment