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

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

问题描述

我有一个滚动视图.在滚动视图中,我添加了许多必要的 UILabels 和 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 文件或故事板)中的视图控制器的主视图在放入界面.您在笔尖编辑器(Interface Builder)中看到的大小只是一个服务建议".因此,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!

看比描述容易,所以从我的书中下载示例代码 并查看当前标题为bk2ch07p367scrollViewInNibAutolayout"和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天全站免登陆