Xcode 9 - “Fixed Width Constraints May Cause Clipping” and Other Localization Warnings

后端 未结 13 1048
失恋的感觉
失恋的感觉 2020-11-27 10:33

I downloaded the new Xcode and in Interface Builder I\'m having a ton of problems with warnings that say things like:

Fixed Width Constraints May Caus

相关标签:
13条回答
  • 2020-11-27 11:14

    I was getting the same warnings even without multiple languages in my app, which led me to find out what was really going on. . .

    There are a few different things going on here. I was able to silence the fixed-width warnings in my own app by changing the width of the object spacings from fixed width to greater than or equal or less than or equal.

    This can be done by selecting the object in interface builder, going to the size inspector and changing it there:


    Or, select the constraint from the document outline, go to size inspector, and change it there:



    As far as the warning at the top of your screenshot:

    Fixed leading and trailing constraints with a center constraint may cause clipping

    Here is a screenshot from my own app in which I was getting the exact same warning:

    I had the label with the @ sign set to leading and trailing to the buttons but also to align the center with the rating label. Once I removed the center alignment constraint, the warning disappeared, but I was left with an improperly laid out set of objects.

    It is then that I resigned myself to embrace the Stack View. As annoying as it is to use, when you get all of the constraints and settings right, it lays out beautifully and with no warnings.


    Edit

    As Repose writes in the comments, sometimes simply adding >= 0 will be what you need, as you are making sure two elements do not overlap.

    0 讨论(0)
  • 2020-11-27 11:16

    As you can see in the image below, I was having the error "Fixed Width Constraints May Cause Clipping" because although I had set my textbox to be vertically centered and my label to have a left margin constraint, I hadn't defined a constraint for the text box in relation to the label, so XCode was alerting me that the textbox could clip (be rendered above) the label.

    After adding the left constraint to the text box to always stay some distance apart from the label the error was considered solved by XCode and it didn't bothered me with the constraint warning anymore.

    0 讨论(0)
  • 2020-11-27 11:17

    Another quick solution !

    For a UIButton by changing the title from plain to Attributed text also resolved my issue:-

    0 讨论(0)
  • 2020-11-27 11:20

    For labels and buttons which are localized this warning makes sense and you should provide the necessary constraints so your labels don't overlap. If they don't overlap now they might in the future, so it won't hurt to provide the constraints.

    Xcode helps you add these constraints automatically:

    In the document outline of your storyboard click on the yellow arrow and either choose "fixed leading" or "fixed trailing", depending on where the text is on your screen (left or right). This will fix it for most issues.

    If you have this issue with a Button without any text (only image), try to remove the "default title" which might still be set for the button:

    0 讨论(0)
  • 2020-11-27 11:23

    To Fix The Error: Fixed Width Constraints May Cause Clipping” and Other Localization You need to select the view/object, go to the "Show Size Inspector", find the Width Constraint and set the Constant to Greater or Equal to:



    To Fix The Error: Leading/Trailing constraint is missing which may cause overlapping with other views

    This means that the view/object Xcode is complaining about, is missing a Leading or Trailing Constraint to a neighboring view.

    While holding control, drag to a near by view/object



    Add a Leading or Trailing Constraint

    0 讨论(0)
  • 2020-11-27 11:26

    You can try Disabling "Respect Language Direction" on per Constraint basis to silence the warning and see if it helps. Select your constraint and open Attributes/Size Inspector. Please see image attached.

    If you are not planning on localizing your app to other languages, then this solution should not have any fallbacks. For localized apps you have to be more conscious of your label and font sizes.

    p.s. This solution works for iOS. For macOS try >= or <= to silence the warning.

    p.p.s. Labels in the picture below are much easier to create using AutoLayout and attributedString property on a single UILabel or UITextView using NSMutableAttributedString. The image is for demonstration purposes only.

    0 讨论(0)
提交回复
热议问题