Ios – “Fixed leading and trailing constraints may cause clipping” bug

constraintsiosstoryboardxcodexcode9

There's this other question here that addresses a similar issue, but in that case Xcode's behavior is correct, just annoying.

In my case, however, I think it's actually a bug:

example

That label can have an unlimited number of lines, so it'll never be clipped, the text will just break.

It works fine with every localization:

example2

I want the label to be centered and I want the text to be at least 20 pixels away from the margins, so I set fixed constraints for the leading and trailing. Xcode wants me to change one of them to a "greater than or equal" constraint, but in that case the text won't be perfectly centered (I tried).

All I want is centered text that won't be too close to the margins, but now I can't accomplish that without triggering that warning.

Any ideas on how to fix this? Is it really a bug, or am I doing something wrong?


Edit: Here's a screenshot showing the error. The constraints aren't directly in a view controller, but in a subview that I use as the backgroun for a table view. If I remove those constraints the warning goes away; if I add them back it shows up again. I tried this in a new project and couldn't reproduce it, I have no idea why it's happening here.

example3

Edit:

Here's another example. When the right constraint is set to "greater than or equal" all's well:

example4

As soon as I set it to "equal", boom:

example5

Best Answer

If you're still looking for an answer, in your particular case, try setting a proportional width on your label and center horizontally in the view controller's view.

Clear all your constraints on the label, right-click drag from the label to the Background View and select Equal Widths, Center Horizontally in Container, and Center Vertically in Container from the popup menu.

Now since you said you wanted 20px margin both to the left and right of your label, edit the Multiplier on the equal width constraint and set it to widthOfBackgroundView - 40)/widthOfBackgroundView.

So let's say the width of your background view is 414, the multiplier is supposed to be 374/414.

The reason for the width minus 40 multiplier is it covers 20px on either sides of the label. This should make your warning go away and layout the label the way you're expecting it to be.

I had the same issue and was able to fix the warnings this way. Hope this helps!