gpt4 book ai didi

ios - iOS9 旋转手机后WKWebView获取旧值Scroll Height

转载 作者:行者123 更新时间:2023-11-30 13:14:28 24 4
gpt4 key购买 nike

我在单元格内有 WKWebView,但问题是当旋转手机时,我得到了 WkWebView 滚动高度的旧值,因此内容无法完全加载。

 override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
self.contentHeights[3] = self.webView.scrollView.contentSize.height
}

此方法更改了表格单元格行的高度,但我得到的值是在旋转之前。

 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
if indexPath.row == 3{
return contentHeights[indexPath.row]
}else{
contentHeights[indexPath.row] = UITableViewAutomaticDimension
return contentHeights[indexPath.row]
}


}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 3{
return contentHeights[indexPath.row]
}else{
return UITableViewAutomaticDimension
}
}

第3行是WkWebView Any Help???

最佳答案

我阅读了几篇文章后发现了两种解决问题的方法。

首先我使用了 KVO

addObserver(self, forKeyPath: "webView.scrollView.contentSize", options: .New, context: nil)

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if keyPath == "webView.scrollView.contentSize" {
if let nsSize = change[NSKeyValueChangeNewKey] as? NSValue {
let height = nsSize.CGSizeValue().height
// Do something here using content height.
}
}}

removeObserver(self, forKeyPath: "webView.scrollView.contentSize", context: nil)

第二种方式是 Javascript 上的事件:

将以下代码添加到html代码中:

<script>$(document).ready(function(){$( document ).resize(function() {webkit.messageHandlers.callbackHandler.postMessage($( document ).height())});});</script>

为WkWebView添加以下配置:

var contentController:WKUserContentController!
override func loadView() {
super.loadView()
contentController = WKUserContentController()
contentController.addScriptMessageHandler(
self,
name: "callbackHandler"
)

config = WKWebViewConfiguration()
config.userContentController = contentController
self.webView = WKWebView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height), configuration: config)
}

添加以下功能:

func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {  
if(message.name == "callbackHandler") {
if !positionOrientationLanscape{
sizeLanscape = CGFloat((message.body as! NSNumber).doubleValue)
contentHeights[4] = sizeLanscape
}else{
sizePortrait = CGFloat((message.body as! NSNumber).doubleValue)
contentHeights[4] = sizePortrait
}
}
}

不要忘记 deinit

deinit {   

self.contentController.removeScriptMessageHandlerForName("callbackHandler")
}

关于ios - iOS9 旋转手机后WKWebView获取旧值Scroll Height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38383772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com