2016-01-24 95 views
0

我實際上正在開發一個iOS應用程序,它必須顯示從服務器下載的HTML代碼。自動調整UIWebView以適應內容

我的視圖是由一個分組的TableView組成,每個部分有一個單元格,單元格由UIWebView組成,它必須顯示HTML代碼。

我不想讓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代碼是從服務器下載,並通過所見即所得的產生從網站(CKEditor的)文本框。)

回答

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 

} 
相關問題