I have designed a part of my user interface in a XIB file using Interface Builder with Autolayout. I created an outlet for one of the layout constraints in the File's Owner class and called it theHatedConstraint
(for no particular reason...). Now all I'm trying to do in my code is to switch off that particular constraint so that the layout machine simply ignores it.
In many other cases I've been able to achieve this behavior by simply setting the constraint's active
property to NO
. In this case however, it just doesn't work. Wherever I call
self.theHatedConstraint.active = NO;
it remains active afterwards as the layout doesn't change in the simulator and the debug view hierarchy (this fancy new 3d debugger thingy in Xcode) still lists the constraint on the respective subview.
I have placed the above code in each of the following methods of my view:
-initWithCoder
-awakeFromNib
-updateConstraints
all without any effect. If I change the constraint's constant instead of deactivating it, it does have the desired effect so I can be sure I have the right constraint and the outlet is set up properly.
The only explanation for this weird behavior is that there must be some kind of mechanism that reactivates theHatedConstraint
after it's been deactivated. So the next thing I tried was to update the constraint state at a time when I can be sure that all the initialization and loading of the view has finished. I happen to have a collection view as a subview in my custom view so the next thing I did was to call the line of code from above whenever the user taps on any of the collection view cells:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
self.theHatedConstraint.active = NO;
}
and finally the constraint was removed from the view.
So here's the big question:
What happens after awakeFromNib
and updateConstraints
have been called in a view, regarding its layout constraints? How can it be that a constraint is still active after I've explicitly set its state to inactive in those methods?
(I'm not touching theHatedConstraint
anywhere else in my code.)