How can I scroll programmatically to the bottom in a UIWebView?
Asked Answered
W

4

10

I know a similar question has been asked before but it seemed never been answered. I have a UIWebView and add some content by string. I use UIWebView because I add some images to it dynamically and also use other HTML features. This example code is simplified.

NSString *myHtmlString = @"SOME LONG TEST STRING 1234567890 123456789 0123456789 0123456789 0123456789 01234567890 WWWWWWWWWW WWWWWWWWWW WWWYYYYYYY YYYYYYYYYY YYYYYYYYYY YYYYYYYYYY YYYYYYYYYY YYYYYYYYY YYYYYYYWWW WWWWWWWWWW WWWWWWWWWW WWWXXXXXXX XXXXXXXXXX XXXZZZZZZZ THIS IS THE END I WANT TO SEE";
NSString *myPath = [[NSBundle mainBundle] bundlePath];
NSURL *myBaseURL = [NSURL fileURLWithPath:myPath];
[myWebView loadHTMLString:myHtmlString baseURL:myBaseURL];

What I want to see is the end of the string. I can scroll there, no problem. But I want to go there programatically.

Welterweight answered 29/8, 2010 at 13:7 Comment(0)
D
26

This is fairly simple. First, you'll need to obtain height of the webpage:

NSInteger height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] intValue];

Now you have the height of the document stored in the height variable. To scroll to bottom you have to use javascript again:

NSString* javascript = [NSString stringWithFormat:@"window.scrollBy(0, %d);", height];   
[webView stringByEvaluatingJavaScriptFromString:javascript];

Of course you need to call them in proper moment. That is

– webViewDidFinishLoad:(UIWebView *)webView

method of your webview delegate.

Declarant answered 29/8, 2010 at 13:36 Comment(4)
Okay. I made it working. The point is that this has to be done in "the" webViewDelegate. I had no web View delegate so I wrote a class and implementing the webViewDelegate. I have put above code in it (NB: it should be "window.scrollBy(0,%d)") and it worked. It really is important to do it AFTER the load finished.Welterweight
The webViewDidFinishLoad always gets called after the webview finished loading the page, so you're completly safe. :) I've corrected the code, thanks for the tip!Declarant
Can't you just scroll to NSIntegerMax? You're now scrolling the page offscreen, as you scroll to {0, pageHeight} so not leaving any content to be shown (then it should have been {0, pageHeight - webViewHeight}). Since it doesn't scroll offscreen, it means that the browser limits the scroll position and thus you may pass NSIntegerMaxDeductible
If you're displaying a PDF or other document in your UIWebView, the document.body.offsetHeight shown above won't return a value. However, you can use window.outerHeight and window.innerHeight instead, as shown here: #1928341Berlauda
F
9

in iOS 5+ you could call the following method from your -(void)webViewDidFinishLoad:(UIWebView *)webView

- (void)webViewScrollToBottom:(UIWebView *)webView
{
    CGFloat scrollHeight = webView.scrollView.contentSize.height - webView.bounds.size.height;
    if (0.0f > scrollHeight)
        scrollHeight = 0.0f;
    webView.scrollView.contentOffset = CGPointMake(0.0f, scrollHeight);
}
Forwhy answered 2/10, 2012 at 22:8 Comment(0)
K
4

This is how you can scroll down Animated:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
        CGPoint bottomOffset = CGPointMake(0, self.myWebView.scrollView.contentSize.height - self.myWebView.scrollView.bounds.size.height);
        [self.myWebView.scrollView setContentOffset:bottomOffset animated:YES];
}
Kristopherkristos answered 8/8, 2013 at 14:17 Comment(0)
C
2

SWIFT 4.x

This is how you can scroll down Animated:

func webViewDidFinishLoad(_ webView: UIWebView) {
    var scrollHeight: CGFloat = webView.scrollView.contentSize.height - webView.bounds.size.height
    if (0.0 > scrollHeight) {
        scrollHeight = 0.0
    }
    webView.scrollView.setContentOffset(CGPoint.init(x: 0.0, y: scrollHeight), animated: true)
}
Corazoncorban answered 23/4, 2018 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.