UIWebView的scrollTo受到返回顶部的阻碍

发布于 2024-12-04 16:34:47 字数 846 浏览 1 评论 0原文

我有一个带有 UIWebView 的 ViewController,位于 iPad 的 Objective-c 项目中。 当我添加 html(使用 loadHtmlString)时,我尝试保留 loadHtml 之前的滚动位置,但我无能为力,它返回到顶部。 以下是我使用的行:

scrollPosition = [[webView stringByEvaluatingJavaScriptFromString: @"scrollY"] intValue];
[webView loadHTMLString:html baseURL:nil];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0, %d);",scrollPosition]];

我在代码中的不同位置测试了值“scrollY” 它停留在我想要的值scrollPosition,但是当它最终显示在屏幕上时,WebView始终位于顶部(scrollY值然后为0)。 有没有人可以帮我弄清楚为什么会发生这种情况?

我在网上以及 StackOverflow 上检查了几个小时的答案。 我尝试了不同的解决方案(例如在我的 webview 中添加 JavaScript“onclick="scroll(); return false;”,但无济于事。 我尝试自我委托 webView 并更新 webViewDidFinishLoad 中的scrollTo,但似乎未调用 webViewDidFinishLoad (未到达断点),尽管我确实在界面中的 .h 和 ViewDidLoad 中的 .m 中添加了以下行: webView.delegate=self;

预先感谢您的帮助。

I have a ViewController with a UIWebView, in a Objective-c project for iPad.
When I add html (with loadHtmlString), I try to keep the scroll position it had before the loadHtml, but I cannot help and it returns to the top.
Here are the lines I use:

scrollPosition = [[webView stringByEvaluatingJavaScriptFromString: @"scrollY"] intValue];
[webView loadHTMLString:html baseURL:nil];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0, %d);",scrollPosition]];

I tested the value 'scrollY' at different locations in my code
and it stays at the value scrollPosition that I want, but when it ends up showing on the screen the WebView is always at the top (with the scrollY value then to 0).
Is there someone who could help me figure out why this happens?

I checked for hours the answers online, also on StackOverflow.
I tried different solutions (like adding in my webview the JavaScript "onclick="scroll(); return false;", but to no avail.
I tried to self-delegate the webView and update the scrollTo in webViewDidFinishLoad, but it seems that webViewDidFinishLoad is not called (the breakpoint is not reached) although I did add in .h in the interface and in the .m in ViewDidLoad the line: webView.delegate=self;

Thank you in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2024-12-11 16:34:47

我意识到重新编译后,webViewDidFinishLoad 在 html 更新后被调用,然后我可以滚动到正确的位置。
对于我在 viewDidLoad 中使用的自委托,它看起来是这样的:

webview.delegate = self;

在更新 webView 中的 html 之前,我使用代码计算滚动位置:

        scrollPosition = [[wbView stringByEvaluatingJavaScriptFromString: @"scrollY"] intValue];

然后,为了在 webView 更新了 html 后移回到该位置,我然​​后使用

-(void) webViewDidFinishLoad:(UIWebView *)wbView {
[wbView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0, %d);",scrollPosition]];
}

:通过下面的帖子找到了部分解决方案,Pawel 的回答是:
如何以编程方式滚动到底部uiwebview

I realized that after recompiling, webViewDidFinishLoad was called after the html was updated, and that I could then scroll to the correct position.
For the self delegation I used in viewDidLoad, it looks so:

webview.delegate = self;

Before updating the html in the webView I calculate the scrolling position with the code:

        scrollPosition = [[wbView stringByEvaluatingJavaScriptFromString: @"scrollY"] intValue];

Then, for moving back to this position once the webView has an updated html, I then use:

-(void) webViewDidFinishLoad:(UIWebView *)wbView {
[wbView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0, %d);",scrollPosition]];
}

I found part of the solution through the post below, with the answer of Pawel:
How can I scroll programmatically to the bottom in a uiwebview

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文