如何让UIWebView滚动到顶部?

时间:2021-09-13 04:29:59

Is there a way to make a UIWebView scroll to the top when I touch say a UISearchView within the same viewController (without using Javascript).

当我在同一个viewController中触摸UISearchView时,有没有办法让UIWebView滚动到顶部(不使用Javascript)。

Something like this:

像这样的东西:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
      [myWebView scrollToTop];    //pseudocode 

}

In other words, what happens when I touch the top bar can also happen programmatically.

换句话说,当我触摸顶部栏时发生的事情也可以通过编程方式发生。

10 个解决方案

#1


5  

If you don't want to use JavaScript there's no public API method to do this. UIWebViews don't inherit from scroll views, so you can't use any of the usual methods. As you've figured out, it's possible to do with JavaScript. You can try to find the actual scroll view in the UIWebView, but it's all undocumented and not really a good thing to do in a production app.

如果您不想使用JavaScript,则没有公共API方法可以执行此操作。 UIWebViews不会从滚动视图继承,因此您无法使用任何常用方法。正如您所知,可以使用JavaScript。你可以尝试在UIWebView中找到实际的滚动视图,但它都是未记录的,在生产应用程序中并不是一件好事。

Update - as of iOS 5 you can now get direct access to a web view's UIScrollView - see Reconquistador's answer for more information.

更新 - 从iOS 5开始,您现在可以直接访问Web视图的UIScrollView - 有关详细信息,请参阅Reconquistador的答案。

#2


24  

CGPoint top = CGPointMake(0, 0); // can also use CGPointZero here
[myWebView.scrollView setContentOffset:top animated:YES];

(Note that if you have set myWebView.scrollView.contentInset.top you will want to take that into account instead of just scrolling to CGPointZero.)

(注意,如果你设置了myWebView.scrollView.contentInset.top,你会想要考虑到这一点,而不是只滚动到CGPointZero。)

#3


7  

Here's a really ugly, terrible way to do this. I'm answering this to show you what never to do.

这是一个非常丑陋,可怕的方法。我正在回答这个问题,告诉你什么都不做。

for (UIView *subview in webView.subviews)
{
    if ([subview isKindOfClass:[UIScrollView class]])
        [(UIScrollView*)subview setContentOffset:CGPointZero animated:YES];
}

#4


6  

Scroll the UIWebView by calling JavaScript through Objective-C:

通过Objective-C调用JavaScript来滚动UIWebView:

NSString *script = @"scrollTo(0, 0)";
[webView stringByEvaluatingJavaScriptFromString:script];

For smooth scrolling, use a library like jQuery:

要平滑滚动,请使用类似jQuery的库:

NSString *script = @"$('html, body').animate({scrollTop:0}, 'slow')";
[webView stringByEvaluatingJavaScriptFromString:script];

#5


4  

Access the UIWebView's scrollview and set YES to method scrollsToTop;

访问UIWebView的scrollview并将YES设置为方法scrollsToTop;

WebView.scrollView.scrollsToTop = YES;

Hope this helps!

希望这可以帮助!

#6


2  

I don't believe you will find any officially supported method to do what you are wanting.

我不相信你会发现任何官方支持的方法来做你想要的。

#7


1  

Swift 3.x

Swift 3.x

webView.scrollView.setContentOffset(CGPoint.zero, animated: true)

#8


0  

This works for me:

这对我有用:

CGPoint topOffset = CGPointMake(0, 0);
//[scrollView setContentOffset: topOffset animated: YES];
[[[webView subviews] lastObject] setContentOffset:topOffset animated:YES];

Where webView is the subclass of the UIWebView, of course.

当然,webView是UIWebView的子类。

#9


0  

why not just touch the status bar of your device. for all the scrollView based controls, tableView, webView, scrollView, there is a : The scroll-to-top gesture is a tap on the status bar; when this property is YES, the scroll view jumps to the top of the content when this gesture occurs. The default value of this property is YES.

为什么不触摸设备的状态栏。对于所有基于scrollView的控件,tableView,webView,scrollView,有一个:滚动到顶部的手势是点击状态栏;当此属性为YES时,滚动视图会在此手势发生时跳转到内容的顶部。此属性的默认值为YES。

#10


-3  

Displaying a PDF in a UIWebView, I found you can jump to the top simply by reloading the document.

在UIWebView中显示PDF,我发现只需重新加载文档就可以跳转到顶部。

#1


5  

If you don't want to use JavaScript there's no public API method to do this. UIWebViews don't inherit from scroll views, so you can't use any of the usual methods. As you've figured out, it's possible to do with JavaScript. You can try to find the actual scroll view in the UIWebView, but it's all undocumented and not really a good thing to do in a production app.

如果您不想使用JavaScript,则没有公共API方法可以执行此操作。 UIWebViews不会从滚动视图继承,因此您无法使用任何常用方法。正如您所知,可以使用JavaScript。你可以尝试在UIWebView中找到实际的滚动视图,但它都是未记录的,在生产应用程序中并不是一件好事。

Update - as of iOS 5 you can now get direct access to a web view's UIScrollView - see Reconquistador's answer for more information.

更新 - 从iOS 5开始,您现在可以直接访问Web视图的UIScrollView - 有关详细信息,请参阅Reconquistador的答案。

#2


24  

CGPoint top = CGPointMake(0, 0); // can also use CGPointZero here
[myWebView.scrollView setContentOffset:top animated:YES];

(Note that if you have set myWebView.scrollView.contentInset.top you will want to take that into account instead of just scrolling to CGPointZero.)

(注意,如果你设置了myWebView.scrollView.contentInset.top,你会想要考虑到这一点,而不是只滚动到CGPointZero。)

#3


7  

Here's a really ugly, terrible way to do this. I'm answering this to show you what never to do.

这是一个非常丑陋,可怕的方法。我正在回答这个问题,告诉你什么都不做。

for (UIView *subview in webView.subviews)
{
    if ([subview isKindOfClass:[UIScrollView class]])
        [(UIScrollView*)subview setContentOffset:CGPointZero animated:YES];
}

#4


6  

Scroll the UIWebView by calling JavaScript through Objective-C:

通过Objective-C调用JavaScript来滚动UIWebView:

NSString *script = @"scrollTo(0, 0)";
[webView stringByEvaluatingJavaScriptFromString:script];

For smooth scrolling, use a library like jQuery:

要平滑滚动,请使用类似jQuery的库:

NSString *script = @"$('html, body').animate({scrollTop:0}, 'slow')";
[webView stringByEvaluatingJavaScriptFromString:script];

#5


4  

Access the UIWebView's scrollview and set YES to method scrollsToTop;

访问UIWebView的scrollview并将YES设置为方法scrollsToTop;

WebView.scrollView.scrollsToTop = YES;

Hope this helps!

希望这可以帮助!

#6


2  

I don't believe you will find any officially supported method to do what you are wanting.

我不相信你会发现任何官方支持的方法来做你想要的。

#7


1  

Swift 3.x

Swift 3.x

webView.scrollView.setContentOffset(CGPoint.zero, animated: true)

#8


0  

This works for me:

这对我有用:

CGPoint topOffset = CGPointMake(0, 0);
//[scrollView setContentOffset: topOffset animated: YES];
[[[webView subviews] lastObject] setContentOffset:topOffset animated:YES];

Where webView is the subclass of the UIWebView, of course.

当然,webView是UIWebView的子类。

#9


0  

why not just touch the status bar of your device. for all the scrollView based controls, tableView, webView, scrollView, there is a : The scroll-to-top gesture is a tap on the status bar; when this property is YES, the scroll view jumps to the top of the content when this gesture occurs. The default value of this property is YES.

为什么不触摸设备的状态栏。对于所有基于scrollView的控件,tableView,webView,scrollView,有一个:滚动到顶部的手势是点击状态栏;当此属性为YES时,滚动视图会在此手势发生时跳转到内容的顶部。此属性的默认值为YES。

#10


-3  

Displaying a PDF in a UIWebView, I found you can jump to the top simply by reloading the document.

在UIWebView中显示PDF,我发现只需重新加载文档就可以跳转到顶部。