Search code examples
iosswiftnslayoutconstraint

Constraint is deactivated by the system


I have a constraint in my view controller that gets deactivated by the system for some reason when the app goes into the background and then coming back to foreground. I haven't been able to pinpoint who's deactivating the constraint. I subclassed NSLayoutConstraint to catch when it's deactivated but the precondition is never triggered. So I'm out of ideas why this is happening and how to prevent it.

@IBOutlet private(set) var testConstraint: TestConstraint!


class TestConstraint: NSLayoutConstraint {
    override var isActive: Bool {
        didSet {
            precondition(isActive, "isActive should not be set to false")
        }
    }
}

Solution

  • If the constraint is not installed (deactivated) in interface builder for some reason it will be deactivated by the system.

    I haven't found any documentation for this but I found this SO post which led me to test to activate the constraint in interface builder and sure enough it stays active when coming from the background.