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:
- I set up
@IBOutlets
for all the constraints that I need to activate/deactivate. - 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. - I do not use
NSLayoutConstraint.deactivate/activate
like you do, I useconstraint.active = YES
/NO
instead. - 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}