When can I activate/deactivate layout constraints?

IosSwiftUiviewAutolayoutNslayoutconstraint

Ios Problem Overview


I've set up multiple sets of constraints in IB, and I'd like to programmatically toggle between them depending on some state. There's a constraintsA outlet collection all of which are marked as installed from IB, and a constraintsB outlet collection all of which are uninstalled in IB.

I can programmatically toggle between the two sets like so:

NSLayoutConstraint.deactivateConstraints(constraintsA)
NSLayoutConstraint.activateConstraints(constraintsB)

But... I can't figure out when to do that. It seems like I should be able to do that once in viewDidLoad, but I can't get that to work. I've tried calling view.updateConstraints() and view.layoutSubviews() after setting the constraints, but to no avail.

I did find that if I set the constraints in viewDidLayoutSubviews everything works as expected. I guess I'd like to know two things...

  1. Why am I getting this behavior?
  2. Is it possible to activate/deactivate constraints from viewDidLoad?

Ios Solutions


Solution 1 - Ios

I activate and deactivate NSLayoutConstraints in viewDidLoad, and I do not have any problems with it. So it does work. There must be a difference in setup between your app and mine :-)

I'll just describe my setup - maybe it can give you a lead:

  1. I set up @IBOutlets for all the constraints that I need to activate/deactivate.
  2. In the ViewController, I save the constraints into class properties that are not weak. The reason for this is that I found that after deactivating a constraint, I could not reactivate it - it was nil. So, it seems to be deleted when deactivated.
  3. I do not use NSLayoutConstraint.deactivate/activate like you do, I use constraint.active = YES/NO instead.
  4. After setting the constraints, I call view.layoutIfNeeded().

Solution 2 - Ios

Maybe you could check your @properties, replace weak with strong.

Sometimes it because active = NO set self.yourConstraint = nil, so that you couldn't use self.yourConstraint again.

Solution 3 - Ios

override func viewDidLayoutSubviews() {
// do it here, after constraints have been materialized
}

Solution 4 - Ios

I believe the problem you are experiencing is due to constraints not being added to their views until AFTER viewDidLoad() is called. You have a number of options:

A) You can connect your layout constraints to an IBOutlet and access them in your code by these references. Since the outlets are connected before viewDidLoad() kicks off, the constraints should be accessible and you can continue to activate and deactivate them there.

B) If you wish to use UIView's constraints() function to access the various constraints you must wait for viewDidLayoutSubviews() to kick off and do it there, since that is the first point after creating a view controller from a nib that it will have any installed constraints. Don't forget to call layoutIfNeeded() when you're done. This does have the disadvantage that the layout pass will be performed twice if there are any changes to apply and you must ensure that there is no possibility that an infinite loop will be triggered.

A quick word of warning: disabled constraints are NOT returned by the constraints() method! This means if you DO disable a constraint with the intention of turning it back on again later you will need to keep a reference to it.

C) You can forget about the storyboard approach and add your constraints manually instead. Since you're doing this in viewDidLoad() I assume that the intention is to only do it once for the full lifetime of the object rather than changing the layout on the fly, so this ought to be an acceptable method.

Solution 5 - Ios

You can also adjust the priority property to "enable" and "disable" them (750 value to enable and 250 to disable for example). For some reason changing the active BOOL didn't had any effect on my UI. No need for layoutIfNeeded and can be set and changed at viewDidLoad or any time after that.

Solution 6 - Ios

The proper time to deactivate unused constraints:

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];

    self.myLittleConstraint.active = NO;
}

Keep in mind that viewWillLayoutSubviews could be called multiple times, so no heavy calculations here, okay?

Note: if you want to reactive some of the constraints later, then always store strong reference to them.

Solution 7 - Ios

When a view is being created the following life cycle methods are called in order:

  1. loadView
  2. viewDidLoad
  3. viewWillAppear
  4. viewWillLayoutSubviews
  5. viewDidLayoutSubviews
  6. viewDidAppear

Now to your questions.

> 1. Why am I getting this behavior?

Answer: Because when you try to set the constraints on the views in viewDidLoad the view does not have its bounds, hence constraints cannot be set. It's only after viewDidLayoutSubviews that the view's bounds are finalized.

> 2. Is it possible to activate/deactivate constraints from viewDidLoad?

Answer: No. Reason explained above.

Solution 8 - Ios

I have found as long as you set up the constraints per normal in the override of - (void)updateConstraints (objective c), with a strong reference for the initiality used active and un-active constraints. And elsewhere in the view cycle deactivate and/or activate what you need, then calling layoutIfNeeded, you should have no issues.

The main thing is not to constantly reuse the override of updateConstraints and to separate the activations of the constraints, as long as you call updateConstraints after your first initialization and layout. It does seem to matter after that where in the view cycle.

Solution 9 - Ios

Enable/disable all constraints in one action:

view.constraints.forEach { constraint in
    constraint.isActive = false
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontybro0103View Question on Stackoverflow
Solution 1 - IosJoachim BøggildView Answer on Stackoverflow
Solution 2 - IosLeoView Answer on Stackoverflow
Solution 3 - IosTony SwiftguyView Answer on Stackoverflow
Solution 4 - IosAshView Answer on Stackoverflow
Solution 5 - Iosuser2387149View Answer on Stackoverflow
Solution 6 - IosLaszloView Answer on Stackoverflow
Solution 7 - IosSumeetView Answer on Stackoverflow
Solution 8 - IoselliotrockView Answer on Stackoverflow
Solution 9 - IosPavel ShorokhovView Answer on Stackoverflow