2

I have a UIWebView that contains a lot of text content. I need to be able to get the location of the UIWebView every time it moves. I am using this code to get the point:

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

now I just need to make it so that this variables value is updated everytime the UIWebView position moves, or there is any scrolling. Is it possible to call a method whenever the UIWebView scrolls?

Jerry
  • 537
  • 1
  • 9
  • 16

4 Answers4

6

You should extend UIWebView, like below

@implementation UIWebView(CustomScroll)
- (void) scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.delegate scrollViewDidScroll: scrollView];
}
@end

and implement scrollViewDidScroll on your controller like:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog("webview scrolled");
}

and don't forget to set your controller to the delegate property of UIWebView.

Deniz Mert Edincik
  • 4,336
  • 22
  • 24
2

I believe that if your header lists your VC as a UIScrollViewDelegate, you can use the -(void)scrollViewDidScroll:(UIScrollView *)scrollView method to find out when it scrolls.

Kolin Krewinkel
  • 1,376
  • 11
  • 12
  • It seems you're correct..I tried multiple techniques to achieve this, failed on all. Sorry. – Kolin Krewinkel Oct 29 '10 at 05:20
  • 1
    This method does work, and is a far better approach. Simply set the scrollview delegate to the view controller too. `[webView.scrollView setDelegate:self]` – Autonomy Jun 23 '13 at 21:45
1

This is not correct. If you will use:

@implementation UIWebView(CustomScroll)
- (void) scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.delegate scrollViewDidScroll: scrollView];
}
@end

You will lose focus when try to enter data on page on iOS7 and more

You need to implement custom class for UIWevView and overwrite scrollViewDidScroll:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView{
[super scrollViewDidScroll:scrollView];
[((id<UIScrollViewDelegate>)self.delegate) scrollViewDidScroll:scrollView];
}
0

if your requirement is to keep loading webview when user scrolls then you should paste your code into

- (void)webViewDidStartLoad:(UIWebView *)webView
Vaibhav Saran
  • 12,848
  • 3
  • 65
  • 75