ios 如何在 UIScrollView 中以编程方式滚动到某个位置

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

How do I scroll to a position programmatically in UIScrollView

iphoneiosuiscrollview

提问by user1072740

I have a scroll view which scrolls horizontally. I just added images, labels and a webview in it and there are many objects in it. I want to directly scroll to the first object in the scroll view. How do I do this?

我有一个水平滚动的滚动视图。我只是在其中添加了图像、标签和 webview,其中有很多对象。我想直接滚动到滚动视图中的第一个对象。我该怎么做呢?

回答by Saurabh Passolia

Use the following code:

使用以下代码:

scrollView.contentOffset = CGPointMake(0,0);

回答by inorganik

To animate your scrolling do this:

要为滚动设置动画,请执行以下操作:

CGRect frame = CGRectMake(0, 768, 1024, 748); //wherever you want to scroll 
[self.scrollView scrollRectToVisible:frame animated:YES];

回答by Kakshil Shah

Easiest way with animation ->

最简单的动画方式 ->

[scrollView setContentOffset:CGPointMake(0, 0) animated:YES]

回答by Arjun Nagineni

The swift 3 version of the answer is:

答案的 swift 3 版本是:

scrollView.contentOffset = CGPoint(x: 0.0, y: 0.0)

scrollView.contentOffset = CGPoint(x: 0.0, y: 0.0)