Search code examples
iosautolayoutnslayoutconstraint

How to use inequality in auto layout constraints?


I find the use of inequality in auto layout tricky (and hard to grasp) because two-constraints per dimension no longer applies. For example, if I want my UIView to be at least 200 pixels wide and centered around X and Y, I still get ambiguous constraint because auto layout cannot figure out how wide the UIView should be. If I specify an additional, exact width constraint, doesn't that defeat the purpose of setting the inequality width constraint in the first place? How do you use inequality constraint in practice?


Solution

  • You would use the inequality with some other constraints at a lower priority. A static width wouldn't be very useful in this case, we're looking more at intrinsic content sizes (so the hugging and compression priorities). So you can have a view which has a minimum width but grows to accommodate additional text for example.

    If you don't have this intrinsic size then the size is ambiguous because it could be anything > 200 and there is no correct answer.