Toma Radu-Petrescu
Toma Radu-Petrescu

Reputation: 2262

How to eliminate “Leading and Trailing constraints … there is already a center constraint” warning

I am trying to add two 1 pixel lines (made of UIViews) near the 'or' UILabel.

Everything looks fine and works as expected, except that I receive the warning from the first image in Interface Builder:

Leading and Trailing constraints with relation "Equal To" can cause unhelpful limitations on the space the text can fill when there is already a center constraint.

I tried changing the constraints from = to >= or <=, but with no success.

Any idea of how to get rid of this warning? Thanks!

Constraints of the first line

Constraints of the label

Constraints of the second line

Upvotes: 3

Views: 935

Answers (2)

rob mayoff
rob mayoff

Reputation: 385600

The warning exists because of the “Align Center X to: Safe Area” constraint on the “or” label. To get rid of the warning:

  1. Delete the “Align Center X to: Safe Area” constraint from the “or” label.
  2. Add an “Equal Width” constraint between the left line and the right line.

With these two changes, the constraint system will still be forced to center the label, but it won't have the center-x constraint that is causing the warning.

Upvotes: 5

LGP
LGP

Reputation: 4333

How about instead of the label, setting an empty UIView between the lines that has the width of the label? Then set the label without left/right constraints centered on the empty view. That way the empty view will adapt to the label without constraining it.

Upvotes: 0

Related Questions