9

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

dbmrq
  • 1,451
  • 13
  • 32
  • can you post the warning you´re receiving? and which xcode are you ussing? – Reinier Melian Sep 14 '17 at 19:30
  • @ReinierMelian Xcode 9; I set it as a tag but forgot to mention in the text. The warning is the one in the title. The complete text says "fixed leading and trailing constraints may cause clipping, one of them should have a constant ≥ standard space". Thanks. :) – dbmrq Sep 14 '17 at 19:33
  • @ReinierMelian If I set one of the constraints to "greater than or equal" the warning goes away, but the text won't be centered on the view. – dbmrq Sep 14 '17 at 19:34
  • If you are convinced you have a bug (and especially if you have an example of the bug) then you should report it to Apple: https://bugreport.apple.com – Robotic Cat Sep 14 '17 at 20:24
  • ok @dbmrq using Xcode 8.3.2 this warning is not present, I don't have Xcode 9 yet – Reinier Melian Sep 14 '17 at 20:25
  • @RoboticCat Yeah, I was just hoping someone would confirm it's actually a bug and maybe offer a workaround. – dbmrq Sep 14 '17 at 20:51
  • @ReinierMelian Yes, it's new in Xcode 9. – dbmrq Sep 14 '17 at 20:51
  • You might try setting one width ≥ and the other ≤ to suppress this warning. Use the same value for each. Haven't found a better solution yet... – atlex2 Nov 09 '17 at 23:39
  • @atlex2 I did try that before asking the question, but it wouldn't work properly, the text would often be out of alignment when running the app. With the latest Xcode updates the problem seems to have disappeared for me though. I'm not sure if it's actually fixed, sometimes weird stuff still shows up, but those warnings that I couldn't get rid of are now gone and the new one's are usually easy to fix. ‍♂️ – dbmrq Nov 10 '17 at 03:10

3 Answers3

6

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!

Xantium
  • 11,201
  • 10
  • 62
  • 89
Pranay
  • 846
  • 6
  • 10
  • Welcome to SO. Please don't ping in answers. Use comments for that. The author of the question will see your answer anyway. – Xantium Dec 06 '17 at 21:56
  • The warnings seem to have disappeared out of the blue, but that’s a good idea, I’ll keep it in mind if it happens again. :) – dbmrq Dec 07 '17 at 01:25
3

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.

shim
  • 9,289
  • 12
  • 69
  • 108
valvoline
  • 7,737
  • 3
  • 47
  • 52
2

Checked in Xcode 9GM (Version 9.0 (9A235)), it works correctly

Xcode screenshot

Remove the constraints and add them manually.

shim
  • 9,289
  • 12
  • 69
  • 108
OMGHaveFun
  • 838
  • 1
  • 10
  • 16