Iulian Onofrei
Iulian Onofrei

Reputation: 9740

NSLayoutConstraint re-activates by itself

I created a strong constraint for a UILabel in a .xib file and an outlet for it.

In my code, I set self.constraint.active = NO, but after the application loads, when I open the Debug View Hierarchy, the same constraint is active.

I tried setting it in viewDidLoad, updateViewConstraints and viewDidLayoutSubviews to no avail.

I also created a NSLayoutConstraint subclass, overwrote the setActive: method and put a breakpoint in it, but only my code triggered it.

Upvotes: 3

Views: 213

Answers (1)

Gil Sand
Gil Sand

Reputation: 6038

When playing with constraints I advise to do it as late as possible. Depending on your case, it might be as early as viewDidLoad.

Whatever you end up doing, you can force a refresh with a layoutIfNeeded call.

You could, if I'm not mistaken, disable your constraint and then force an update of constraints / layout ( updateConstraints setNeedsLayout) and then call layoutIfNeeded. But I think you only need to modify your constraint and doing so will automatically set your view to relayout itself whenever it has the time OR when you call layoutIfNeeded.

Either way, try with just these two lines, otherwise, add the ones I put between parenthesis.

I'm not gonna lie, sometimes constraints are part iOS, part skill, part black magic.

Upvotes: 2

Related Questions