8

I keep references to my NSLayoutConstraint

var flag = true
@IBOutlet weak var myConstraint: NSLayoutConstraint!

Then for some @IBAction I activate/deactivate depending on my flag variable:

@IBAction func tapped(sender: UIButton) {
    flag = !flag
    UIView.animateWithDuration(1.0) {
        if self.flag {
            NSLayoutConstraint.activateConstraints([self. myConstraint])
        } else {
            NSLayoutConstraint.deactivateConstraints([self. myConstraint])
        }
    }
}

But when I call my action once again, I have an error unexpectedly found nil while unwrapping an Optional value for myConstrain.

More over it doesn't animate. What am I doing wrong?

I follow tutorial from WWDC 2015:

enter image description here

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • 3
    keep a strong reference of object in IBOutlet declaration. `@IBOutlet strong var myConstraint: NSLayoutConstraint!` – Gandalf Jul 11 '15 at 15:01
  • 3
    Gandalf's comment is correct. deactivating a constraint must cause the view hierarchy to release it's ownership of it. if you don't keep a strong reference, it will be deallocated. @Gandalf, you should make this an answer so the OP can accept it. – Duncan C Jul 11 '15 at 15:41

1 Answers1

21

Deactivating a constraint is same as calling removeConstraint: for a view. Refer the documentation. So when you remove an object which has weak reference will cause the object deallocation for it. Now this object is nil and activating it won't have any effect at all. To solve the issue you need to have a strong reference to constraint object.

@IBOutlet strong var myConstraint: NSLayoutConstraint!
Gandalf
  • 2,399
  • 2
  • 15
  • 19