Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why weak IBOutlet NSLayoutConstraint turns to nil when I make it inactive?

I have an IBOutlet NSLayoutConstraint in my app. Very simple:

@property (nonatomic, weak) IBOutlet NSLayoutConstraint* leftConstraint; 

At some point I want to deactivate this constraint:

self.leftConstraint.active = NO; 

It happens in a method called from cellForRowAtIndexPath:. And the constraint becomes nil right after the line above. However if I declare this property as strong then it's ok, it doesn't turn to nil. Can anybody explain why it happens?

like image 728
Andrey Chernukha Avatar asked Jun 27 '16 10:06

Andrey Chernukha


2 Answers

When your outlet is weak, the only strong reference is from the view's constraints property. Deactivating the constraint removes it from that array, so there are no more strong references.

like image 179
Avi Avatar answered Sep 30 '22 08:09

Avi


A possible workaround would be to change the constraint priority instead of making it inactive:

@property (nonatomic, weak) IBOutlet NSLayoutConstraint* leftConstraint; (...) self.leftConstraint.priority = 250; 

Then, self.leftConstraint is not disposed.

EDIT:

Xcode does not support changing priority for required (=1000) constraints, so be sure you switch in a range between 1...999.

like image 45
Fran Pugl Avatar answered Sep 30 '22 07:09

Fran Pugl