设置宽度等于scrollview时,UIView会忽略我的约束吗? [英] UIView ignoring my constraints when setting width equal to scrollview?
问题描述
每次将视图的宽度设置为滚动视图时,都会将绿色方块从屏幕上推开.
Every time I set the width of my view to my scroll view it pushes the green square off the screen.
推荐答案
@belgrim我在使用滚动视图时遇到了麻烦.我想分享我的工作.
@belgrim I had had a rough time working with scroll views. I would like to share my work around.
- 首先,将滚动视图拖动到情节提要中,然后固定到四个侧面.
- 然后,在滚动视图内添加一个视图,并将其固定在其超级视图的四个侧面(即滚动视图)
- 结果看起来像这样
(不必担心红色标记,这些标记将在后续步骤中解决)
- 添加约束以使视图的宽度等于其父视图(滚动视图)的宽度
结果如下:
- 现在,最重要的部分是,我们需要在视图内部添加内容,并应在视图内部正确定义y约束,以便滚动视图根据内容滚动.在这里,我简单地添加一个标签并将约束定义为:
(此外,我将行数设置为0,将换行符设置为自动换行)
(Also, I setup the number of lines as 0 and line break as wordwrap)
最后,我们获得了正确定义的故事板:
Finally, we get properly defined storyboard:
在标签出口中定义了长文本,我们得到的最终输出为:
With long text defined in the label outlet, the final output we get:
我展示了一个添加标签的简单示例,您可以根据需要添加其他视图,只需确保不要错过定义y约束.
I have shown a simple example adding a label, you can add other views as per your need, just make sure that you don't miss defining the y-constraints.
希望这会有所帮助.
这篇关于设置宽度等于scrollview时,UIView会忽略我的约束吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!