NSLayoutConstraint constant not updating after setting NSLayoutConstraint constant not updating after setting ios ios

NSLayoutConstraint constant not updating after setting

You will need to call setNeedsUpdateConstraints method of corresponding UIView(control) of which your NSLayoutConstraint is present to update constraint.

For example of UIButton

self.buttonConstraint.constant = 55;[self.btnTest setNeedsUpdateConstraints];

In your case

[self setNeedsUpdateConstraints];

I ran into a problem where the first time I set the constraint constant in code the constant would be set but then after calling layoutIfNeeded(), such as this:

myConstraint.constant = newValue;[view layoutIfNeeded]

the constant would go back to the original value! I could watch the value change back in the debugger.

I finally figured out that it was because, in the storyboard, I had set a variation on the constant. For example:

enter image description here

As soon as I removed the variation, the constraint constant value did not change back to the original value with layoutIfNeeded call.

Are you sure this constraint is not de-activated somewhere?Setting the "active" property of a constraint to false causes the view hierarchy to remove the constraint. If you don’t also have a reference to it, the constraint object will be deleted from memory.

I had the same issue as you, and removed the "weak" so the constraint is now a strong property. Consequently, it's not set to nil when de-activated (because my view controller always has a strong pointer to it), and I can re-activate it and re-set its constant.

// NB: don't create these contraints outlets as "weak" if you intend to de-activate them, otherwise they would be set to nil when deactivated!@IBOutlet private var captionViewHeightConstraint: NSLayoutConstraint!