如何在 iOS 上制作垂直滚动视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15063636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:26:05  来源:igfitidea点击:

How to make a vertical scrolling view on iOS?

iosuiscrollviewvertical-scrolling

提问by Anil Varghese

Can someone please guide me to a tutorial , on how to implement a vertical scrolling view on my iOS app?

有人可以指导我学习如何在我的 iOS 应用程序上实现垂直滚动视图的教程吗?

I can't believe that the last 2 days , I can't find a single working example on just a vertical scrolling view. All the tutorials are about horizontal scrolling, zooming etc etc.

我不敢相信过去 2 天,我在垂直滚动视图上找不到单个工作示例。所有的教程都是关于水平滚动、缩放等。

What I am looking for would be something like this tutorial http://www.edumobile.org/iphone/iphone-programming-tutorials/scrollview-example-in-iphone-2/, which shows how to make a uiscrollView and add objects to it, from the Builder and not programmatically. The only flaw I found was , that when trying to add a map down in the scrolling area, the map would appear up and not in the position I placed it. Maybe any ideas on that too?

我正在寻找的是类似于本教程http://www.edumobile.org/iphone/iphone-programming-tutorials/scrollview-example-in-iphone-2/ 的内容,它展示了如何制作 uiscrollView 并添加对象到它,从 Builder 而不是以编程方式。我发现的唯一缺陷是,当尝试在滚动区域向下添加地图时,地图会向上显示而不是在我放置它的位置。也许对此也有任何想法?

Anything in mind?

有什么想法吗?

回答by Anil Varghese

So you want to create a scroll view in xib.

所以你想在xib中创建一个滚动视图。

  1. Add a scroll view to the nib file
  2. set its size to to whatever you want

    enter image description here

  3. Add your controls (Buttons,labels..)

    enter image description here

  4. Add it as a sub view of main view

    enter image description here

  5. Finally create a property of scroll view and in viewDidLoadset the content size of the scroll view

     self.scrollView.contentSize =CGSizeMake(320, 700);
    
  1. 向 nib 文件添加滚动视图
  2. 将其大小设置为您想要的任何值

    在此处输入图片说明

  3. 添加您的控件(按钮、标签..)

    在此处输入图片说明

  4. 将其添加为主视图的子视图

    在此处输入图片说明

  5. 最后创建滚动视图的属性并viewDidLoad设置滚动视图的内容大小

     self.scrollView.contentSize =CGSizeMake(320, 700);
    

回答by Atif Khan

It simple, same as horizontal scroll view. Add a scrollview in a view hierarchy,

它很简单,与水平滚动视图相同。在视图层次结构中添加滚动视图,

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:scrollView];

Now to make scroll view scrollable, set its scrollview content size greater than its bound.

现在要使滚动视图可滚动,请将其滚动视图内容大小设置为大于其边界。

[scrollView setContentSize:CGSizeMake(scrollView.bounds.size.width, scrollView.bounds.size.height*3)];

Now it can scroll three time the height of scrollView.

现在它可以滚动三倍于 scrollView 的高度。

回答by Matt Perejda

I've struggled with this simple issue for a full day. There are most likely benefits to a more sophisticated approach, but unchecking autolayout solved my issue.

我一整天都在为这个简单的问题而苦苦挣扎。更复杂的方法很可能有好处,但取消选中自动布局解决了我的问题。

For the quick fix, I believe this is the best approach and will save you a HUGE headache.

对于快速修复,我相信这是最好的方法,并且会为您省去一个巨大的麻烦。

回答by Matt Perejda

Just change the content size of your scrollview.Let's assume you are creating a UIScrollView with the following frame

只需更改滚动视图的内容大小。假设您正在使用以下框架创建 UIScrollView

scl=[[UIScrollView  alloc]initWithFrame:CGRectMake(0, 0, 620, 172)];

Now call the content size property of your scroll view,like i have shown below .

现在调用滚动视图的内容大小属性,如下所示。

[scl setContentSize:CGSizeMake(700, 172)];

Now add the scroll view on a view / View Controller etc and check it .

现在在视图/视图控制器等上添加滚动视图并检查它。