Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can I activate/deactivate layout constraints?

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?
like image 446
tybro0103 Avatar asked Dec 15 '14 22:12

tybro0103


People also ask

What is Nslayoutconstraint?

The relationship between two user interface objects that must be satisfied by the constraint-based layout system.


3 Answers

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().
like image 81
Joachim Bøggild Avatar answered Nov 16 '22 00:11

Joachim Bøggild


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.

like image 45
Leo Avatar answered Nov 16 '22 00:11

Leo


override func viewDidLayoutSubviews() {
// do it here, after constraints have been materialized
}
like image 21
Tony Swiftguy Avatar answered Nov 16 '22 00:11

Tony Swiftguy