自动调整UIWebView以适应内容

时间:2016-01-24 16:56:43

标签: ios swift uiwebview resize

我实际上正在开发一个必须显示从服务器下载的HTML代码的iOS应用程序。

我的视图由一个Grouped TableView组成,每个部分有一个单元格,单元格由一个必须显示HTML代码的UIWebView组成。

我不希望UIWebView可以向上/向下滚动,我只想允许右/左滚动,我希望将其设置为其内容的高度。

我阅读了很多关于这个问题的主题,但任何解决方案对我来说都是完全正常的。

根据我的说法,奇怪的是,对于某些细胞,我得到了正确的高度并且它被正确设置但不是来自每个细胞......

这是我尝试的代码(仍然编写了几个测试):

func webViewDidFinishLoad(webView: UIWebView) {
    webView.scrollView.scrollEnabled = false

    let size = self.sizeThatFits(CGSizeMake(self.frame.width, CGFloat(FLT_MAX)))
    self.frame = CGRectMake(0, 0, self.frame.width, size.height);

    let test1 = webView.stringByEvaluatingJavaScriptFromString("document.body.offsetHeight;")
    let test2 = webView.stringByEvaluatingJavaScriptFromString("document.body.scrollHeight;")
    let test3 =  webView.stringByEvaluatingJavaScriptFromString("document.documentElement.scrollHeight;")

    let height = CGFloat(Double(test2!)!)

    if(self.containingVC is InfoDrugViewController){
        let VC = self.containingVC as! InfoDrugViewController
        VC.heightReady(height, index: self.index.section)
    }else{ if (self.containingVC is InfoProtocolViewController){
        let VC = self.containingVC as! InfoProtocolViewController
        VC.heightReady(height, index: self.index.section)
        }
    }

}

如果有人知道如何设法始终保持正确的高度,那将是完美的!

(请注意,HTML代码是从服务器下载的,并且是由网站上的WYSIWYG(CKEditor)文本字段生成的。)

1 个答案:

答案 0 :(得分:0)

它找到了一个可行的解决方案,也许它不是最干净的解决方案,但至少它起作用了:

func webViewDidFinishLoad(webView: UIWebView) {
    webView.scrollView.scrollEnabled = false
    var frame = self.frame
    frame.size.width = self.containingVC.view.frame.size.width
    frame.size.height = 1
    self.frame = frame
    frame.size.height = self.scrollView.contentSize.height
    self.frame = frame

    let test0 = self.scrollView.contentSize.height

    let size = self.sizeThatFits(CGSizeMake(self.frame.width, CGFloat(FLT_MAX)))
    self.frame = CGRectMake(0, 0, self.frame.width, size.height);

    let test1 = CGFloat(Double(webView.stringByEvaluatingJavaScriptFromString("document.body.offsetHeight;")!)!)
    let test2 = CGFloat(Double(webView.stringByEvaluatingJavaScriptFromString("document.body.scrollHeight;")!)!)
    let test3 = CGFloat(Double(webView.stringByEvaluatingJavaScriptFromString("document.body.clientHeight")!)!)
    let test4 = CGFloat(Double(webView.stringByEvaluatingJavaScriptFromString("document.documentElement.scrollHeight;")!)!)
    let test5 = CGFloat(Double(webView.stringByEvaluatingJavaScriptFromString("document.documentElement.clientHeight")!)!)
    let test6 = CGFloat(Double(webView.stringByEvaluatingJavaScriptFromString("document.documentElement.offsetHeight")!)!)

    print("0: \(test0) | 1: \(test1) | 2: \(test2) | 3: \(test3) | 4: \(test4) | 5: \(test5) | 6: \(test6)")


    let height = max(test0, test1, test2, test3, test4, test5, test6)

    if(self.containingVC is InfoDrugViewController){
        let VC = self.containingVC as! InfoDrugViewController
        VC.heightReady(height, index: self.index.section)
    }else{ if (self.containingVC is InfoProtocolViewController){
        let VC = self.containingVC as! InfoProtocolViewController
        VC.heightReady(height, index: self.index.section)
        }
    }
    self.scrollView.scrollEnabled = true

}