When can I activate/deactivate layout constraints? When can I activate/deactivate layout constraints? ios ios

When can I activate/deactivate layout constraints?


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().


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.


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