将ScrollView添加到现有视图 [英] Add a ScrollView to existing View
问题描述
我正在Swift 2.0中开发一个小应用程序。我有一个具有以下层次结构的视图:
I'm developing a little app in Swift 2.0. I have a View with the following hierarchy:
现在,放置在此视图中的元素无法完全显示在其中,因此我想使用 ScrollView
以便能够滚动所有内容。
Now, the elements placed in this view can't be displayed entirely in it, so I would like to use a ScrollView
in order to be able to scroll all the content.
如何将 View
的所有内容嵌入到新的 ScrollView
?我可以通过代码以编程方式执行此操作吗?
How I can embed all the content of my View
to a new ScrollView
? Can I do this programmatically by code?
推荐答案
更新:有一种更简单的方法可以做到这一点我没有知道我何时发布了这个答案
1)转到故事板上的viewcontroller,然后点击其中的内容并按 Command + A
。这将选择视图中的所有元素。
1) Go to the viewcontroller on the storyboard and click on something in it and press Command + A
. This will select all the elements in the view.
2)转到编辑器 - >嵌入 - >滚动视图
。这会将所有标签,视图等嵌入到scrollView中。
2) Go to Editor -> Embed In -> Scroll View
. This will embed all the labels, views, etc into a scrollView.
3)选择 scrollview
即可嵌入式。按 Control
并将其拖到viewcontroller的相应类文件中并创建一个插座 scrollView
。
3) Select the scrollview
that you just embedded. Press Control
and drag it to the respective class file of the viewcontroller and create an outlet scrollView
.
4)将此添加到您的 viewDidLoad()
4) Add this to your viewDidLoad()
scrollView.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height+100)
确保 contentSize的高度超过您的视图尺寸。
手动添加SCROLLVIEW
1)将 scrollview
从对象库拖放到<$故事板中的c $ c> viewcontroller 并以'scrollView'为您的程序创建一个插座。
1) Drag and drop a scrollview
from the Object Library onto your viewcontroller
in the storyboard and create an outlet to your program as 'scrollView'.
2)点击您的 viewcontroller
然后转到尺寸检查器并记下宽度和高度。
2) Click on your viewcontroller
and go to the size inspector and note down the width and height.
3)点击 scrollview
并设置宽度
和 height
与的ViewController
。并将 X
和 Y
值设为0
3) Click on your scrollview
and set the width
and height
the same as the viewcontroller
. and set the X
and Y
values to 0
4)点击 scrollView
并将其拖到一边
4) Click on the scrollView
and drag it a little bit to the side
5)按 Command + A
选择所有元素,包括 scrollView
。按命令
并单击 scrollView
取消选择 ScrollView
5) Press Command+A
to select all the elements including scrollView
. Press Command
and click on the scrollView
to deselect the ScrollView
6)您现在将选择除 scrollView
之外的所有元素。现在单击并将它们拖动到scrollView中。
6)You will have all the elements except the scrollView
selected now. Now click and drag them into the scrollView.
7 )现在点击 scrollView
并设置 X
和 Y
大小检查器中的值为0.
7) Now click on the scrollView
and set the X
and Y
values to 0 from the Size Inspector.
8)将其添加到 viewDidLoad()
scrollView.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height+100)
确保contentSize的高度超过视图的大小。
这应该为您的视图创建一个scrollView。某些元素可能处于稍微不同的位置。你可以通过在storyBoard上移动它们来轻松修复它们。
That should create a scrollView for your view. Some of the elements might be in a slightly different position. You can easily fix them by moving them on your storyBoard.
这篇关于将ScrollView添加到现有视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!