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...
- Why am I getting this behavior?
- Is it possible to activate/deactivate constraints from viewDidLoad?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…