2

I would like to add an URL/google bar on top of my webview, and access it by scrolling up my web page exactly as Safari do. To do this, I would detect when the user is scrolling the page, and more over when the scroll reaches the top.

But I really don't know how.

An idea ? Thanx a lot.

Martin

Martin
  • 11,881
  • 6
  • 64
  • 110

3 Answers3

1

I was also digging for the same and found a solution. I coded the following method to detect the scrolling coordinates. See below:

-(void)userDidScrollWebView:(id)scrollPoint{
    // NSLog(@"scrolled:::");

    NSString *x1 = [webView stringByEvaluatingJavaScriptFromString: @"scrollX"];


    NSString *y1 = [webView stringByEvaluatingJavaScriptFromString: @"scrollY"];


    NSLog(@"scroll x=%@ y=%@", x1,y1);      

    if ([y1 isEqualToString: @"0"]) {
        NSLog(@"RELAOD ME");
    }   
}

Hope it helps you.

Shraddha
  • 1,052
  • 11
  • 22
  • 1
    One year after, I succeded to log again ! It funny cause I used the same solution you preconize :) – Martin Feb 07 '12 at 15:09
0

Yep, you want to implement the UIScrollViewDelegate for your UIWebView.

David Sowsy
  • 1,680
  • 14
  • 13
  • But UIWebView does not implement the UIScrollViewDelegate ! There are already some discutions about, and they say that it's only possible with javascript... (http://stackoverflow.com/questions/1604795/scrolloffset-in-uiwebview) – Martin Feb 01 '10 at 15:39
  • 1
    Just a slight terminology nitpick: UIWebView *implements* UIScrollViewDelegate (it declares conformity to that protocol, after all). What you meant is that it doesn't *have* a UIScrollViewDelegate (and the appropriate setDelegate/delegate methods with which you could provide one -- which makes sense, because it is not a UIScrollView). – uliwitness Jun 29 '11 at 17:40
  • A quick check with [someWebView conformsToProtocol:@protocol(UIScrollViewDelegate)] shows that for all intents and purposes UIWebView is a UIScrollView. You have to write the methods to override the default behavior. – David Sowsy Jul 04 '11 at 13:14
  • Maybe David means you can subclass UIWebView and override UIScrollViewDelegate methods. It's a good way. Many developers get so used to delegation that they forget about subclassing. – javieralog Jan 18 '12 at 09:40
0

You can use the following methods to solve your problem.

For getting the pageOffset:

int pageYOffset = [[webViewObj stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];

For getting the total scroll height of a webpage:

int scrollHeight = [[webViewObj stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] intValue];

For scrolling the webpage to a particular offset:

[webViewObj stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.scrollTop = %d",scrollHeight ]];

Biranchi
  • 16,120
  • 23
  • 124
  • 161