ios Xcode 9 - “固定宽度约束可能导致裁剪”和其他本地化警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45122691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:19:54  来源:igfitidea点击:

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

ioslocalizationinterface-builderxcode9-beta

提问by Dave G

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

我下载了新的 Xcode,在 Interface Builder 中,我遇到了很多警告问题,例如:

Fixed Width Constraints May Cause Clipping

固定宽度约束可能导致裁剪

It looks like this:

它看起来像这样:

enter image description here

在此处输入图片说明

I do have localization for several languages and I understand the warning that in another language a label's size may change, but my app doesn't have this problem. I ran and tested it in Xcode 8 yesterday, it was fine. I don't want to spend hours and hours adding pointless new constraints.

我确实有多种语言的本地化,我理解警告,即在另一种语言中,标签的大小可能会改变,但我的应用程序没有这个问题。我昨天在 Xcode 8 中运行并测试了它,它很好。我不想花费大量时间添加毫无意义的新约束。

Any suggested solutions?

任何建议的解决方案?

回答by Shades

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 warningsin my own app by changing the width of the object spacingsfrom fixed width to greater than or equalor less than or equal.

这里发生了一些不同的事情。通过将对象间距宽度从固定宽度更改为大于或等于小于或等于,我能够在我自己的应用程序中消除固定宽度警告

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

这可以通过在界面构建器中选择对象,转到大小检查器并在那里更改来完成:

enter image description here

在此处输入图片说明



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

或者,从文档大纲中选择约束,转到大小检查器,并在那里更改它:

enter image description here

在此处输入图片说明





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:

这是我自己的应用程序的屏幕截图,其中我收到了完全相同的警告:

enter image description here

在此处输入图片说明

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.

就在那时,我辞职接受 Stack View。尽管使用起来很烦人,但当您正确设置所有约束和设置时,它会布局精美且没有警告。



Edit

编辑

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

正如Repose在评论中所写的那样,有时只需添加 >= 0 就是您所需要的,因为您要确保两个元素不重叠。

回答by Repose

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.

您可以尝试在每个约束的基础上禁用“尊重语言方向”以消除警告并查看它是否有帮助。选择您的约束并打开 Attributes/Size Inspector。请参阅附图。

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.

ps 此解决方案适用于 iOS。对于 macOS,请尝试 >= 或 <= 使警告静音。

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.

使用 NSMutableAttributedString 在单个 UILabel 或 UITextView 上使用 AutoLayout 和 attributesString 属性创建下图中的 pps 标签要容易得多。该图像仅用于演示目的。

Disrespect Language Direction

Disrespect Language Direction

回答by Pascal

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:

Xcode 帮助您自动添加这些约束:

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.

在故事板的文档大纲中,单击黄色箭头,然后根据文本在屏幕上的位置(左侧或右侧)选择“固定前导”或“固定尾随”。这将解决大多数问题。

Xcode screenshot

Xcode screenshot

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:

如果没有任何文本(只有图像)的按钮出现此问题,请尝试删除可能仍为按钮设置的“默认标题”:

Xcode screenshot

Xcode screenshot

回答by Lionking

With Labels, you can set Linesis 0and Autoshrinkproperties is Minimum Font Sizeto remove Fixed Width Constraints May Cause Clippingwarnings, like this:

使用Labels,您可以设置Linesis0AutoshrinkpropertiesMinimum Font Size来删除Fixed Width Constraints May Cause Clipping警告,如下所示:

enter image description here

enter image description here

回答by tryKuldeepTanwar

Another quick solution !

另一个快速解决方案!

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

对于 UIButton,通过将标题从纯文本更改为属性文本也解决了我的问题:-

enter image description here

enter image description here

回答by reojased

I know this question has already been answered but what I did to fix this error in my case was to add the "Aspect ratio" property and then eliminate the width or height constraint this worked pretty well and was less effort, and I managed to keep the same output and adapt my view for the different devices.

我知道这个问题已经得到了回答,但在我的情况下,我为解决这个错误所做的是添加“纵横比”属性,然后消除宽度或高度约束,这工作得很好而且工作量更少,我设法保持相同的输出并调整我对不同设备的看法。

回答by Alessandro Ornano

Swift 4 , Xcode 9.1 :

斯威夫特 4,Xcode 9.1:

About this issue, I think your object don't know what it's the correct center position in the context of it's superview, and using remove, greater thanor other leading/trealing settings most of times don't work correctly. First, you must check the correct constraints of your superview.

关于这个问题,我认为您的对象不知道它在其superview上下文中的正确中心位置,并且使用删除、大于或其他前导/trealing 设置在大多数情况下无法正常工作。首先,您必须检查您的超级视图的正确约束。

If your superview/s are correctly setted, you can try to "explain" to your object what is the correct position in the view by setting the "horizontally in Container"constraint:

如果您的 superview/s 设置正确,您可以尝试通过设置“水平在容器中”约束向您的对象“解释”视图中的正确位置:

enter image description here

enter image description here

回答by ChikabuZ

If you need fixed width constraint for button just set width constraint priority to 700.

如果您需要按钮的固定宽度约束,只需将宽度约束优先级设置为700.

回答by Tom Linton

I had the same problem when moving to Xcode 9 and found an approach that's useful for certain kinds of layouts. In my case, I wanted a table header in which two columns (UILabels) were of fixed width and another was of variable width. Regardless of how I specified column widths (including using constraints greater than or equal instead of equal, etc.), I kept getting the warning about possible clipping. In my case, I wanted the variable width column (UILabel) to clip if necessary. I could have just ignored the warning, but don't like doing that.

我在迁移到 Xcode 9 时遇到了同样的问题,并发现了一种对某些类型的布局有用的方法。就我而言,我想要一个表格标题,其中两列 (UILabels) 的宽度固定,另一列的宽度可变。不管我如何指定列宽(包括使用大于或等于而不是等于等的约束),我一直收到有关可能裁剪的警告。就我而言,如果需要,我希望可变宽度列 (UILabel) 进行剪辑。我本可以忽略警告,但不喜欢那样做。

The approach that worked here was to create a UIView with appropriate size constraints and embed the UILabel as a subview in the UIView. Then truncation happens if necessary and I get no warning. This works whether the UIView/embedded UILabel is in a StackView or not.

在这里工作的方法是创建一个具有适当大小约束的 UIView,并将 UILabel 作为子视图嵌入到 UIView 中。然后在必要时发生截断,我没有收到任何警告。无论 UIView/嵌入式 UILabel 是否在 StackView 中,这都有效。

This is essentially the same approach as that of Haroldo Gondim but here you can see it also works with or without StackView.

这基本上与 Haroldo Gondim 的方法相同,但在这里您可以看到它在使用或不使用 StackView 的情况下都可以使用。

The following image shows the approach, with and without StackView. "SpacerName" is a variable width UIView containing a label and "SpacerPD" is one with a fixed width of 80. [Colors are not significant; just there to show where the views are.]

下图显示了使用和不使用 StackView 的方法。“SpacerName”是一个包含标签的可变宽度 UIView,而“SpacerPD”是一个固定宽度为 80 的 UIView。 [颜色不重要;只是为了显示视图的位置。]

enter image description here

enter image description here

回答by Haroldo Gondim

I had the same problem, but when I changing to >=it automatically set the constant to 0, if I choose 60for instance, the warning appears again. So I was in a loop with the problem.

我遇到了同样的问题,但是当我更改为>=它时会自动将常量设置为060例如,如果我选择,警告再次出现。所以我陷入了这个问题的循环中。

I could fix embedding my Labelin a View

我可以修复嵌入我LabelView

Editor > Embed In > View

In LabelI set Top, Bottom, Leadingand Trailingwith constant = 0

Label我集TopBottomLeadingTrailingconstant = 0

constraints

constraints

In ViewI set the constraintsthat I was expecting before.

View我设置了constraints我之前期待的。