uiwebview高度计算迅速 [英] uiwebview height calculation in swift
问题描述
我有一个滚动视图,并想向其中动态添加多个Web视图.需要获取webview的高度.我用下面的代码.但是它并不总是有效.有时显示的高度不正确. func webViewDidFinishLoad(aWebView:UIWebView) {
i have a scrollview and want to add multiple webviews dynamically into it. need to get height of webview. i have used below code. but it does't work always.some times showing wrong height. func webViewDidFinishLoad(aWebView: UIWebView) {
let screenSize = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
var frame = aWebView.frame
frame.size.height = 1
frame.size.width = screenSize.width
aWebView.frame = frame
let fittingSize = aWebView.sizeThatFits(CGSizeZero)
frame.size = fittingSize
aWebView.frame = frame
}
推荐答案
这是一个非常好的问题,使Web视图适合其内容具有有趣的应用程序,例如使用适合以下条件的Web视图在地图上显示信息窗口其内容.
This is a really good question, fitting web views to their content has interesting applications, such as showing an info windows on a map using a web view that fits to its content.
这是使用WKWebView
的解决方案,首先创建一个1 x 1像素大小的Web视图,加载其内容,并查询Web视图以找到其内容大小,然后再相应地更新Web视图框架的大小.有点hacky,但这似乎是唯一的方法.
Here is a solution using WKWebView
that begins by creating a 1-by-1px size web view, loading its content, and querying the web view to find its content size before updating the web view frame size accordingly. Slightly hacky but seems to be the only way.
import WebKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
view.addSubview(webView)
webView.navigationDelegate = self
let htmlFile = Bundle.main.path(forResource: "index", ofType: "html")
let html = try! String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
webView.loadHTMLString(html, baseURL: nil)
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
var frame = webView.frame
webView.evaluateJavaScript("document.documentElement.offsetWidth", completionHandler: { ret, _ in
guard let width = ret as? CGFloat else { return }
print("setting width: ", width)
frame.size.width = width
webView.frame = frame
webView.evaluateJavaScript("document.documentElement.offsetHeight", completionHandler: { ret, _ in
guard let height = ret as? CGFloat else { return }
print("setting height: ", height)
frame.size.height = height
webView.frame = frame
})
})
}
}
我的示例HTML文件(index.html
):
My sample HTML file (index.html
):
<html>
<head>
<meta name="viewport" content="width=150, initial-scale=1.0">
<style>
html, body { margin: 0; padding: 0 };
</style>
</head>
<body>
<div style="height:280px;width:150px;background-color:cyan;"></div>
</body>
</html>
注意:如果可能,您应该尝试使用WKWebView
而不是UIWebView
. UIWebView
解决方案可能是可行的,但我认为WKWebView
应该更好地满足您的需求是不值得的时间和精力的.
Note: You should try to use the WKWebView
instead of the UIWebView
if possible. A UIWebView
solution may be possible, but I don't think it's worthy of the time and effort when the WKWebView
should fit your needs better.
这篇关于uiwebview高度计算迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!