簡體   English   中英

自動調整UIWebView的大小以適合內容

[英]Autoresize of UIWebView to fit content

我實際上正在開發一個iOS App,該App必須顯示從服務器下載的HTML代碼。

我的視圖由一個Grouped TableView組成,每個節一個單元,而該單元僅由UIWebView組成,該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代碼是從服務器下載的,是由網站的WYSIWYG(CKEditor)文本字段生成的。)

它找到了一個可行的解決方案,也許這不是最干凈的解決方案,但至少它能起作用:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM