如何向我的 iOS 应用程序添加滚动视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1040519/
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 16:04:10  来源:igfitidea点击:

How can I add a scroll view to my iOS application?

iosiphonexcodeuiscrollview

提问by teabot

I am creating an application for the iPhone which involves having buttons which animate a image view, I created another view in which the buttons are held, however I would like to be able to horizontally or vertically, scroll this view of buttons. I have looked into adding scroll views and I have even followed a tutorial, But I just can't figure out how to add it to my app. Any Ideas anyone? In the tutorial I followed the guy adds the scroll view to the MainWindow.xib, now my app is created and designed in the viewcontroller.xib. So after following the tutorial when I hit build and go it loads that nib and all you see is a white background and a scroll view. (also in that tutorial he scrolled an image where as I would like to scroll a long view of buttons) I realize the newbie-ness of my question could be crazy, but I'm new to programming all together. Here's another additional question, if someone helped me get this scroll view working where would you design the contents of that scrolled view if it's length is longer than the iphone screen? Because It is my understanding that the space to design with in interface builder is the size of the iphone screen and designing interfaces longer than the iphone screen isn't do-able. (of course I know it is do-able I just don't know how to do it).

我正在为 iPhone 创建一个应用程序,其中包含为图像视图设置动画的按钮,我创建了另一个包含按钮的视图,但是我希望能够水平或垂直滚动​​此按钮视图。我研究过添加滚动视图,我什至遵循了教程,但我不知道如何将它添加到我的应用程序中。任何想法?在我跟随的教程中,家伙将滚动视图添加到 MainWindow.xib,现在我的应用程序是在 viewcontroller.xib 中创建和设计的。因此,当我点击构建并执行教程后,​​它会加载该笔尖,您看到的只是白色背景和滚动视图。(也在那个教程中,他滚动了一张图片,因为我想滚动按钮的长视图)我意识到我的问题的新手可能很疯狂,但我' 我是一起编程的新手。这是另一个额外的问题,如果有人帮助我让这个滚动视图工作,如果它的长度比 iPhone 屏幕长,你会在哪里设计滚动视图的内容?因为我的理解是在界面构建器中设计的空间是 iphone 屏幕的大小,设计比 iphone 屏幕长的界面是不可行的。(当然我知道这是可行的,但我不知道该怎么做)。s长度比iphone屏幕还长?因为我的理解是在界面构建器中设计的空间是 iphone 屏幕的大小,设计比 iphone 屏幕长的界面是不可行的。(当然我知道这是可行的,但我不知道该怎么做)。s长度比iphone屏幕还长?因为我的理解是在界面构建器中设计的空间是 iphone 屏幕的大小,设计比 iphone 屏幕长的界面是不可行的。(当然我知道这是可行的,但我不知道该怎么做)。

回答by teabot

You can set the content view (scrolling area) dimensions programmatically:

您可以以编程方式设置内容视图(滚动区域)尺寸:

[scrollView setContentSize:CGSizeMake(contentViewWidth, contentViewHeight)];

And then add you view components to the scroll view using the coordinate system of the content view. So say your scroll view was 100x100 pixels and you wanted the scroll view to be twice as wide:

然后使用内容视图的坐标系将您的视图组件添加到滚动视图。因此,假设您的滚动视图是 100x100 像素,并且您希望滚动视图的宽度是原来的两倍:

[scrollView setContentSize:CGSizeMake(200.0f, 100.0f)];

You could then add a button to appear on each half of the scroll view:

然后,您可以添加一个按钮以显示在滚动视图的每一半:

// First half of content view
button1.frame.origin.x = 10.0f;
button1.frame.origin.y = 50.0f;
[scrollView addSubView:button1];

// First half of content view
button2.frame.origin.x = 110.0f; // NOTE that 110 is outside of
button2.frame.origin.y = 50.0f;  // the scroll view frame
[scrollView addSubView:button2];

You could probablydesign each page of the scroll view as a separate sub view in the NIB and then shift the sub views origin to the correct page location either in Interface Builder or programmatically - but I have never tried this. If this works you'd then be able to layout your buttons using IB.

你可以大概设计滚动视图的每一页作为NIB一个独立的子视图,然后在Interface Builder或以编程方式要么转移子视图原点到正确的页面位置-但我从来没有尝试这样做。如果这有效,您就可以使用 IB 来布局您的按钮。

I would be interested to know if this works.

我很想知道这是否有效。

回答by teabot

most views have a .hidden property.

大多数视图都有一个 .hidden 属性。

set it to YES to hide -> myscrollview.hidden = YES; set to NO to show -> myscrollview.hidden = NO;

将其设置为 YES 以隐藏 -> myscrollview.hidden = YES; 设置为 NO 以显示 -> myscrollview.hidden = NO;

you can put this hide/show inside an animation and fade out the scrollview.

您可以将此隐藏/显示放在动画中并淡出滚动视图。

look-up "simple quartz animation" - it only a few lines of code.

查找“简单的石英动画”——它只有几行代码。