将UI元素添加到UIScrollView吗? [英] Add UI elements to UIScrollView?

查看:81
本文介绍了将UI元素添加到UIScrollView吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个滚动视图.在滚动视图中,我添加了许多必要的UILabel和UIButtons等.

I have a scroll view. On the scroll view I have added many necessary UILabels and UIButtons etcs.

现在,我想添加更多标签和按钮,但是我在视图界面上没有空间可以添加它们,否则它们将与其他元素重叠.如何添加更多UI元素,以便在用户滚动时可以与之交互?我在想,如果将UIelement放在滚动视图的底部,滚动条将向下滚动"并为我提供更多空间.

Now I want to add MORE labels and buttons but I have no room on the interface of the view to add them, otherwise they will overlap the other elements. How do I add more UI elements so that they can be interacted with when the user scrolls? I was thinking if I hold the UIelement over the bottom of the scroll view, the scroll would "scroll down" and present me with more space.

推荐答案

此处的关键是要记住,将nib(.xib文件或情节提要)中视图控制器的主视图放进去时,无论如何总是要调整其大小.界面.您在笔尖编辑器(接口生成器)中看到的大小只是服务建议".因此,SamirChen的答案是正确的:将视图设置为Freeform,以便您可以调整大小,并使其足够大以容纳本身足够大的滚动视图,以便您可以放入所有想要的内容.

The key thing here is to remember that a view controller's main view in a nib (.xib file or storyboard) is always resized anyway when it is put into the interface. The size you see it at in the nib editor (Interface Builder) is just a "serving suggestion". Thus, SamirChen's answer is quite right: set the view to Freeform so you can resize, and just make it big enough to hold the scroll view which itself is big enough so that you can put in all the desired contents.

我只想补充两点,对您有所帮助:

I would add just two points that you will find helpful:

  • 在滚动视图外使用自动布局.调整视图大小以适合界面时,这将导致滚动视图变为正确的大小,并且将导致其他所有内容正确地重新放置.

  • Use autolayout outside the scroll view. This will cause the scroll view to become the right size when the view is resized to fit the interface, and will cause everything else to be repositioned correctly.

在滚动视图中也使用自动布局!这是真的很酷的部分.如果您在滚动视图的所有内容和滚动视图本身(其超级视图)之间设置了足够的约束,则将自动使用从内而外的约束为您计算contentSize!因此,您不必知道内容大小或在代码中进行设置!

Use autolayout inside the scroll view too! This is the really cool part. If you set up sufficient constraints between all the contents of the scroll view and the scroll view itself (their superview), the contentSize will be calculated for you automatically using constraints from the inside out! Thus, you don't have to know the content size or set it in code!

看起来比描述起来容易,因此请从我的书中下载示例代码,然后查看当前标题为"bk2ch07p367scrollViewInNibAutolayout2"和"bk2ch07p367scrollViewInNibAutolayout2"的示例.

It is easier to see than to describe, so download the example code from my book and look at the examples currently entitled "bk2ch07p367scrollViewInNibAutolayout" and "bk2ch07p367scrollViewInNibAutolayout2".

这篇关于将UI元素添加到UIScrollView吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆