objective-c 如何在 iPhone 上使用滚动视图?

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

How to use scroll view on iPhone?

iphoneobjective-ccocoa-touchuikituiscrollview

提问by Chilly Zhong

I want to display a text with a lot of lines. I added a multiline-label to a scroll view, but it didn't show anything. Looks like this is not the correct way to use the scroll view. How to use scroll view so that users can drag down to see more text?

我想显示有很多行的文本。我向滚动视图添加了多行标签,但它没有显示任何内容。看起来这不是使用滚动视图的正确方法。如何使用滚动视图让用户可以向下拖动以查看更多文本?

回答by Rog

Apple's UIScollView documentationis quite good, you should start there and understand the class.

Apple 的UIScollView 文档非常好,您应该从那里开始并了解该类。

Think of a Scrollview as a large view surface over which the screen slides*. You can add subviews to the scrollview and then scrolling is like positioning the screen over the scrollview - the screen acts like a window you can see through to part of the scroll view's content below.

将 Scrollview 视为屏幕滑动的大视图表面*。您可以向滚动视图添加子视图,然后滚动就像将屏幕定位在滚动视图上 - 屏幕就像一个窗口,您可以看到下面滚动视图的部分内容。

To do this, a scrollview has a few extra properties over a normal UIView - but it is like a UIView in one imprtant respect: it doesn't render any content directly itself. You need to add a subview to draw your text. A scrollview is set up and displayed in exactly the same way as a UIView - i.e. you set the frame add it to another view and to show your text you need to add subviews to the UIScrollView that can actualy render the text.

为此,滚动视图比普通 UIView 有一些额外的属性——但在一个重要方面,它就像一个 UIView:它本身不直接呈现任何内容。您需要添加一个子视图来绘制文本。滚动视图的设置和显示方式与 UIView 完全相同 - 即您设置框架将其添加到另一个视图并显示您的文本,您需要将子视图添加到 UIScrollView 可以实际呈现文本。

In order to set up a basic UIScrollView you request, you should just create it like a normal full screen view - set the frame to be the same size as the window and add the scrollview to the window as a subview. Then make a large UITextView to hold your text. The UIText view can be as large as you like - specifically it can be bigger than the screen. set the contentSize property of the UIScrollView to be the same as the frame of the UITextView and then add the UIText view as a subview of the UIScrollView.

为了设置您请求的基本 UIScrollView,您应该像普通的全屏视图一样创建它 - 将框架设置为与窗口相同的大小,并将滚动视图作为子视图添加到窗口中。然后制作一个大的 UITextView 来保存您的文本。UIText 视图可以大到你喜欢的大小——特别是它可以比屏幕大。将 UIScrollView 的 contentSize 属性设置为与 UITextView 的 frame 相同,然后将 UIText 视图添加为 UIScrollView 的子视图。

Once you have this working, you can move the content automatically with the contentOffset property, controll zooming and set up a delegate to observe scrolling events.

完成此操作后,您可以使用 contentOffset 属性自动移动内容,控制缩放并设置委托以观察滚动事件。

* more accurately, over which the frame slides but I'm assuming you are making a full screen UIScrollView. I'm sure you can generalise it if you want a smaller view.

* 更准确地说,框架在其上方滑动,但我假设您正在制作全屏 UIScrollView。如果你想要一个更小的视图,我相信你可以概括它。

回答by Andy Bourassa

You can just use a UITextView. It's a child of UIScrollView, and if you set it's text property to an amount of text that does not fit within it's frame, the view will become scrollable.

您可以只使用 UITextView。它是 UIScrollView 的子项,如果将它的 text 属性设置为不适合其框架的文本量,则视图将变为可滚动的。

回答by Victor Konshin

I used UIScrollView this way for an instructions pane in one of my applications. It worked but I was unhappy with the result. I then switched to using a UIWebView and have been much happier with the result. It handles sizing the scroll view automatically, I get all the formatting capabilities of HTML and I can have links that go to additional information (or external sites) with no more than adding another HTML file and maybe some delegate code. Here is the code I used (and I added the HTML file to my app as a resource):

我以这种方式将 UIScrollView 用于我的一个应用程序中的说明窗格。它奏效了,但我对结果不满意。然后我切换到使用 UIWebView 并且对结果更满意。它自动处理滚动视图的大小,我获得了 HTML 的所有格式化功能,我可以拥有指向附加信息(或外部站点)的链接,只需添加另一个 HTML 文件和一些委托代码。这是我使用的代码(我将 HTML 文件作为资源添加到我的应用程序中):

NSString *path = [[NSBundle mainBundle] pathForResource:@"instructions" ofType:@"html"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];
NSString *htmlString = [[NSString alloc] initWithData: [readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
[self.instructionsView loadHTMLString:htmlString baseURL:nil];

回答by Susan

U may refer to this website http://www.edumobile.org/iphone/iphone-programming-tutorials/scrollview-example-in-iphone/

你可以参考这个网站http://www.edumobile.org/iphone/iphone-programming-tutorials/scrollview-example-in-iphone/

U follow the step by step to make the scroll view in iphone...good luck:)

你按照一步一步在iphone中制作滚动视图......祝你好运:)