fre*_*dpi 9 ios swift wkwebview
在我用Swift 2.1编写的iOS应用程序中,我使用WKWebView加载一个HTML字符串,该字符串是使用JSON解析器从wordpress博客加载的.
我实现了委托方法func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {并将WKWebView的导航委托设置为self,以便处理链接按下.
现在,当我打开包含WKWebView的ViewController时,这个委托方法被调用一次,这是你在加载webView时所期望的行为 - 所以委托似乎设置得当.
我现在的问题是,大多数时候webView包含的链接都不可点击.当您按下链接时,通常会出现灰色背景,如下图所示.但大多数时候,当我按下链接时,灰色背景不会出现,所以当我修改时,委托方法不会被调用.这个问题肯定不需要对错误配置做些什么WKNavigationDelegate,因为有时链接选择正常工作(大约10%).
您是否知道为什么链接随机有时无法点击,有时可点击(10%的情况)?
解决方案是删除contentInset. 不幸的是,WKWebView它的设计不是为了处理它的使用(当contentInset为负数时),因此我们必须通过简单地向 htmlString 添加一个空来使用解决方法div,就像这样:
<div style='width:100%;height:100px'></div>
Run Code Online (Sandbox Code Playgroud)
如果应该在不重新加载整个页面的情况下更改底部插图,则可以首先div在底部插入一个巨大的 ,然后通过添加一个正数来补偿它contentInset。这正是这个扩展的作用:
extension WKWebView {
/// The maximum possible height of the webView bottom content inset
@nonobjc static let maxBottomHeight: CGFloat = 20000
/// Provides an easy way to set the real bottom inset of the webView. Due to a bug in WKWebView framework, we first insert an emtpy div with the size WKWebView.maxContentSize on the bottom of the webView and compensate it by setting a positive contentInset (should be positive, otherwise the bug will be there again).
func set(bottomInset inset: CGFloat) {
self.scrollView.contentInset.bottom = inset - WKWebView.maxBottomHeight
}
}
Run Code Online (Sandbox Code Playgroud)
div不要忘记在 html 字符串末尾添加:
let htmlFooter = "<div style='width:100%;height:\(String(describing: WKWebView.maxBottomHeight))px'></div></body></html>"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3496 次 |
| 最近记录: |