WKWebView goBack 带我到本地 html 页面的顶部 [英] WKWebView goBack takes me to top of local html page

查看:50
本文介绍了WKWebView goBack 带我到本地 html 页面的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 html 页面作为应用内帮助,我使用 WKWebView 来显示它们,其中包括一个返回"按钮调用 goBack.

I have some html pages as in-app help and I use a WKWebView to show them, which includes a "back" button calling goBack.

一切都很顺利,除了后退按钮总是把你带到上一个本地 html 页面的最顶部(而不是你点击的链接所在页面的滚动位置),尽管如果你遵循进一步的链接它工作正常远程内容并使用返回"从一个远程页面转到上一个远程页面.

Everything works smoothly, except the back button always takes you to the very top of the previous local html page (instead of the scroll position of the page where the link you clicked was), although it works fine if you follow further links to remote content and use "back" to go from one remote page to the previous remote page.

我已经删除了所有额外的东西来调试这个,比如帖子导航等,所以目前我只是打电话给:

I have removed everything extra to debug this, like post navigation etc, so currently I am simply making a call to:

[webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"help" withExtension:@"html"]]];

然后只需 [webView goBack] 并返回页面顶部.如果您遵循指向另一个内部 html 或外部页面的链接,则无关紧要.但是,正如我所说,如果您点击一个链接到一个外部页面,然后是另一个外部页面,那么第一个返回会将您带到第一个外部页面的正确位置.

Then just [webView goBack] and back to the top of the page you go. It doesn't matter if you follow a link to another internal html or an external page. But, as I said if you follow a link to an external page and then another external page, the first back takes you to the correct position of the first external page.

有什么想法吗?它使浏览内部 html 变得烦人.

Any ideas? It makes browsing internal html annoying.

好的,它可能与实际的 html 有关 - 有一个页面可以返回工作,试图找出差异. 之前的所有内容都是相同的,并且所有页面都通过了 w3.org html4 过渡验证.

OK, it might have to do with the actual html - there is one page where back works, trying to figure out the difference. Everything before the <body> is the same and all pages pass w3.org html4 transitional validation.

推荐答案

这是一个暂定的答案,如果有人可以改进它或解释机制,我将不胜感激.似乎是goBack"如果该页面的内容中有一个元素的宽度大于 webview 试图将内容流入的原始宽度,则将您带到上一页的顶部. 例如,在我的情况下,因为我使用的是动态字体,对于一些文本大小一些标题和一些链接太长.直接的解决方案是添加body {word-wrap:break-word;}.这有点锤子"解决方案,因为它确实有一些意想不到的后果,所以我正在尝试寻找替代方案(目前正在使用 viewport 元,以防万一).

Here is a tentative answer, I'd appreciate it if someone can improve on it or explain the mechanics. It seems that "goBack" takes you to the top of the previous page if that page has an element in its content whose width is greater than the original width the webview tried to flow the content in. In my case for example, as I am using Dynamic Type fonts, for some text sizes some headers and some links were too long. The immediate solution is to add body {word-wrap:break-word;}. This is a bit of a "hammer" solution as it does have some unintended consequences, so I am trying to find an alternative (currently playing with the viewport meta in case it can help).

我还没有找到修复 loadHTMLStringgoBack 行为的方法 - 当我尝试首先将字符串写入文件时,goBack确实有效,但我还有其他问题(无法再点击我的包中的 html 文件链接).

I have not found a way to fix goBack behaviour for loadHTMLString - when I try to first write the string to a file, goBack does work, but I have other issues (can no longer follow links to html files in my bundle).

这篇关于WKWebView goBack 带我到本地 html 页面的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆