简体   繁体   English

如何在UIWebView中以编程方式滚动到底部?

[英]How can I scroll programmatically to the bottom in a UIWebView?

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. 我有一个UIWebView并按字符串添加一些内容。 I use UIWebView because I add some images to it dynamically and also use other HTML features. 我使用UIWebView,因为我动态地添加了一些图像,并使用其他HTML功能。 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. 但我想以编程方式去那里。

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. 现在,您将文档的高度存储在height变量中。 To scroll to bottom you have to use javascript again: 要滚动到底部,您必须再次使用javascript:

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. 您的webview委托的方法。

Hope this was helpful, Pawel 帕维尔希望这很有帮助

in iOS 5+ you could call the following method from your -(void)webViewDidFinishLoad:(UIWebView *)webView 在iOS 5+中,您可以从-(void)webViewDidFinishLoad:(UIWebView *)webView调用以下方法-(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);
}

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

SWIFT 4.x 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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM