UILabel不会使用AutoLayout在UIScrollView中自动换行 [英] UILabel won't word wrap inside UIScrollView with AutoLayout
问题描述
我在 UIScrollView
中有一个 UILabel
,我试图自动换行。我想使用AutoLayout进行布局配置。 UILabel
字不在 UIScrollView
内时完美包装。我只需要将行数设置为0并将换行模式设置为自动换行。
I have a UILabel
inside a UIScrollView
that I am trying to word wrap. I want to use AutoLayout for the layout configuration. The UILabel
word wraps perfectly when it is not inside a UIScrollView
. I just have to set the number of lines to 0 and set the line break mode to word wrap.
然而,当我将 UILabel
放入 UIScrollView
,包装词不再起作用。我已经检查了 UIScrollView中的AutoLayout动态UILabel高度和使用Interface Builder和Autolayout正确地在UIScrollView中调整UILabel?,但这两个问题都没有帮助我的答案。
However, when I put the UILabel
inside a UIScrollView
, the word wrapping ceases to work. I have already checked "Dynamic UILabel height inside UIScrollView with AutoLayout" and "Correctly Size UILabel inside UIScrollView using Interface Builder and Autolayout?", but neither of those questions have answers that help me.
我有设置测试项目如果有人想要看一下,我正在说明我正在谈论的内容。
I have set up a test project illustrating exactly what I am talking about if someone wants to have a look at it.
推荐答案
原因是滚动内的约束视图,固定在滚动视图,并不意味着你认为他们的意思。它们与从内到外确定滚动视图的 contentSize
有关;他们自己不会从外面确定滚动视图中内容的大小。请参阅我的回答: https:// stackoverflow。 com / a / 13548039/341994
The reason is that constraints inside a scroll view, pinned to the scroll view, do not mean what you think they mean. They have to do with determining the contentSize
of the scroll view from the inside out; they do not of themselves determine the size of the things inside the scrollview from the outside in. See my answer here: https://stackoverflow.com/a/13548039/341994
在您的情况下,最简单的解决方案是使用中间内容视图,以便您具有以下层次结构:
The simplest solution in your case is to use an intermediate content view, so that you have this hierarchy:
scroll view
content view
label
现在给出内容视图的绝对宽度和高度约束,并将它在四个边上固定到滚动视图。 现在标签将按预期工作。内容视图有足够的约束来绝对设置自己的大小,并从内部约束滚动视图的 contentSize
。同时,对标签的约束不是滚动视图而是内容视图,并且由于内容视图具有固定大小并且是普通视图(不是滚动视图),因此标签上的约束符合您的预期。
Now give the content view absolute width and height constraints and pin it on all four sides to the scroll view. Now the label will work as you expect. The content view has sufficient constraints both to set its own size absolutely and to constrain the scroll view's contentSize
from the inside. The constraints on the label, meanwhile, are not to the scroll view but to the content view, and since the content view has a fixed size and is a normal view (not a scroll view), the constraints on the label do what you expect.
这篇关于UILabel不会使用AutoLayout在UIScrollView中自动换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!