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:
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:
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.
Edit:
Here's another example. When the right constraint is set to "greater than or equal" all's well:
As soon as I set it to "equal", boom:
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!
Some problem here, introduced with the final version of Xcode 9. Seems that the bug is related to the language direction: for some reason Interface Builder doesn't understand quite well the autolayout for the default language direction conditions.
As suggested by Repose on this thread you should try to invalidate the use of language direction for at least one of the failing constraints (ie. trailing). This approach solved my warnings.
Checked in Xcode 9GM (Version 9.0 (9A235)), it works correctly
Remove the constraints and add them manually.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With