Question

I have two buttons, a scroll up and a scroll down that are used to scroll the webpage in a UIWebView. The only issue is, once they hit the top or bottom of the page they can keep scrolling. They can scroll until the entire webpage is off of the UIWebView. Does anyone know what code I need to plug in to prevent this from happening? I've been researching for a while now and it is all going over my head. Is it even possible to detect where the webpage has ended? This is the code I'm using to scroll, I have a feeling I need to throw an if statement in here to check if it has hit the top of the webpage.

    //Scroll Up:
-(IBAction)scrollContentUp:(id)sender {

    [_viewWeb.scrollView setContentOffset:CGPointMake(0,_viewWeb.scrollView.contentOffset.y - 40.0) animated:YES];

}

//Scroll down:
-(IBAction)scrollContentDown:(id)sender {
    [_viewWeb.scrollView setContentOffset:CGPointMake(0,_viewWeb.scrollView.contentOffset.y + 40.0) animated:YES];

}

By the way, it's an iPad only app.

Was it helpful?

Solution

+You need to do something like:

   //Scroll Up:
-(IBAction)scrollContentUp:(id)sender {
    if(_webView.scrollView.contentOffset.y - 40 >= 0) {
        [_viewWeb.scrollView setContentOffset:CGPointMake(0,
                                    _viewWeb.scrollView.contentOffset.y - 40.0) 
                                     animated:YES];
    }
}

//Scroll down:
-(IBAction)scrollContentDown:(id)sender {
    if(_webView.scrollView.contentOffset.y + _webView.frame.size.height + 40 <= _viewWeb.scrollView.contentSize.height ) {
            [_viewWeb.scrollView setContentOffset:CGPointMake(0,
                                        _viewWeb.scrollView.contentOffset.y + 40.0) 
                                         animated:YES];
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top