我在单元格中有WKWebView,但问题是当旋转手机时,我得到了WkWebView Scroll Height的旧值,因此内容不会完全加载。
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 ???
答案 0 :(得分:0)
我读了几篇帖子后,我找到了解决问题的两种方法。
首先我使用了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")
}