Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inactive constraint becomes active?

Tags:

ios

autolayout

I've made a constraint in my storyboard and connected to a variable in my view controller. In updateViewConstraints I set the constraint's active property to false.

When I do

self.view.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()

in viewDidLoad, layoutIfNeeded calls updateViewConstraints which sets the constraint to not active, but then somewhere after this it sets the constraint to active and then UIViewAlertForUnsatisfiableConstraints symbolic breakpoint is hit due to conflict with my other constraints. When I check the value of .active further up the stack at this breakpoint it is true.

This only happens in viewDidLoad. Before the view is displayed on the screen updateViewConstraints is called again, and this time setting the constraint to not active sticks and the layout is correct. I still get the conflicting constraints error in the console. Is this a bug?

Edit: Here's the stack trace leading to setting the constraint to active: enter image description here

like image 741
Nick Avatar asked Oct 20 '22 16:10

Nick


1 Answers

The active property is actually a virtual property that just indicates if the constraint is installed. If you set it true, it installs itself into the nearest common ancestor of the two views. So it was becoming active because in storyboard it was installed in the view. To fix my problem, I simply unticked Installed in storyboard.

like image 124
Nick Avatar answered Oct 22 '22 19:10

Nick