Search code examples
swiftxcodeautolayoutuikit

Constraints from code behave differently from interface builder


I'm moving part of UI to code from Interface Builder. After finishing, I'm comparing different branches in Debug View Hierarchy mode - the set of constraints look the same, except different constraints are grayed:

Before:

enter image description here

After:

enter image description here

What does this gray color mean? They all active. The issue is that the grayed constraint on the second picture doesn't work


Solution

  • The gray color highlights the active constraints that Auto Layout is not using due to conflicts or because there are other higher priority constraints it is using.

    The problem is that your constraints aren't quite the same.

    Before:

    superview.bottom = self.bottom + 200 @ 750
    

    After:

    self.bottom = superview.bottom + 200 @ 750
    

    Those aren't the same. To fix the After case, you either need to switch the self.bottom and superview.bottom items or change the constant to -200.