MegaManX MegaManX - 5 months ago 26
iOS Question

Adding non active constraints to existing UIView

I have

UIView
for which I have set constraints in IB. Now in code, i want to do some animation, by adding some new constraints and deactivating old. First I add new constraint to view, that is inactive, like this:

// camera view new constraints
self.cameraNewLeftConstraint = [NSLayoutConstraint constraintWithItem:self.cameraView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.smallImageContainerView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
self.cameraNewLeftConstraint.active = NO;
[self.presentationView addConstraint:self.cameraNewLeftConstraint];


But this generates constraint warning during runtime (stnadard message - will attempt to break some constraints), that I have conflicting constraints, although I have added inactive constraint. How can I deal with this, how is this supposed to be done?

Answer

Views don't have inactive constraints. If you add a constraint to a view, then that constraint will be active.

The new method of setting the active property to YES adds the constraint to the proper view for you. In your code, when you called addConstraint, iOS added your constraint to your view, and changed active to YES.

So, don't call addConstraint on the view. Just create the constraint and set its active property to YES when you want it to be used.

Comments