Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make UIWebView scroll to the top?

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).

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.

like image 406
Gazzer Avatar asked Mar 02 '11 20:03

Gazzer


2 Answers

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.)

like image 178
Reconquistador Avatar answered Nov 14 '22 21:11

Reconquistador


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];
}
like image 29
Mark Adams Avatar answered Nov 14 '22 20:11

Mark Adams