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

编程入门 行业动态 更新时间:2024-10-25 13:25:26
本文介绍了如何以编程方式滚动到 UIWebView 的底部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道以前有人问过类似的问题,但似乎从未得到回答.我有一个 UIWebView 并按字符串添加一些内容.我使用 UIWebView 是因为我动态地向其中添加了一些图像并且还使用了其他 HTML 功能.此示例代码已简化.

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 = @ 一些长期测试字符串1234567890 123456789 0123456789 0123456789 0123456789 01234567890 WWWWWWWWWW WWWWWWWWWW WWWYYYYYYY YYYYYYYYYY YYYYYYYYYY YYYYYYYYYY YYYYYYYYYY YYYYYYYYY YYYYYYYWWW WWWWWWWWWW WWWWWWWWWW WWWXXXXXXX XXXXXXXXXX XXXZZZZZZZ THIS IS我想看END";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];

现在您将文档的高度存储在 height 变量中.要滚动到底部,您必须再次使用 javascript:

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

你的 webview 委托的方法.

method of your webview delegate.

更多推荐

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

本文发布于:2023-11-11 17:57:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579160.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   UIWebView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!