【问题标题】:WKWebView get the old value Scroll Height after rotate the phone iOS9旋转手机iOS9后WKWebView获取旧值Scroll Height
【发布时间】:2016-07-14 20:32:14
【问题描述】:

我在一个单元格中有 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???

【问题讨论】:

    标签: ios swift wkwebview screen-rotation


    【解决方案1】:

    我看了几篇帖子,然后我找到了两种解决问题的方法。

    首先我使用了 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")
        }
    

    【讨论】:

      猜你喜欢
      • 2015-12-20
      • 1970-01-01
      • 2016-11-21
      • 2016-07-06
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-04
      相关资源
      最近更新 更多