Detect when I scroll my UIWebView at top of the pa

2019-02-19 08:31发布

问题:

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

回答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.



回答2:

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



回答3:

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 ]];