向 UIScrollView 添加 UI 元素?

Add UI elements to UIScrollView?(向 UIScrollView 添加 UI 元素?)

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

问题描述

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

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

现在我想添加 MORE 标签和按钮,但是我在视图的界面上没有空间添加它们,否则它们会与其他元素重叠.如何添加更多 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 文件或故事板)中的主视图在放入界面.您在 nib 编辑器(界面生成器)中看到的大小只是一个服务建议".因此,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".

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

本文标题为:向 UIScrollView 添加 UI 元素?

基础教程推荐