xcode “固定的前导和尾随约束可能会导致剪辑”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46226756/
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
"Fixed leading and trailing constraints may cause clipping" bug?
提问by dbmrq
There's this other questionhere that addresses a similar issue, but in that case Xcode's behavior is correct, just annoying.
这里有另一个问题解决了类似的问题,但在这种情况下,Xcode 的行为是正确的,只是令人讨厌。
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).
我希望标签居中,并且希望文本距边距至少 20 像素,因此我为前导和尾随设置了固定约束。Xcode 希望我将其中一个更改为“大于或等于”约束,但在这种情况下,文本不会完全居中(我试过)。
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:
一旦我将它设置为“相等”,繁荣:
回答by Pranay
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.
清除所有限制的标签,从标签上单击鼠标右键拖动到Background View
和选择Equal Widths
,Center Horizontally in Container
以及Center Vertically in Container
从弹出菜单中。
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
.
现在,既然您说您希望标签的左侧和右侧都有 20 像素的边距,请编辑Multiplier
等宽约束上的 并将其设置为widthOfBackgroundView - 40)/widthOfBackgroundView
。
So let's say the width of your background view is 414
, the multiplier is supposed to be 374/414
.
因此,假设您的背景视图的宽度是414
,乘数应该是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.
width minus 40
乘数的原因是它覆盖了标签两侧的 20px。这应该会使您的警告消失并按照您期望的方式布置标签。
I had the same issue and was able to fix the warnings this way. Hope this helps!
我遇到了同样的问题,并且能够以这种方式修复警告。希望这可以帮助!
回答by valvoline
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.
这里有一些问题,在 Xcode 9 的最终版本中引入。似乎该错误与语言方向有关:由于某种原因,Interface Builder 不太了解默认语言方向条件的自动布局。
As suggested by Reposeon this threadyou should try to invalidate the use of language direction for at least one of the failing constraints (ie. trailing). This approach solved my warnings.
正如Repose在此线程上所建议的那样,您应该尝试使语言方向的使用对至少一个失败的约束(即尾随)无效。这种方法解决了我的警告。