gpt4 book ai didi

ios - 自动调整 UIWebView 的大小以适应内容

转载 作者:行者123 更新时间:2023-11-30 13:45:37 28 4
gpt4 key购买 nike

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

我的 View 由分组 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) 文本字段生成。)

最佳答案

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

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

}

关于ios - 自动调整 UIWebView 的大小以适应内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34978624/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com