ios - WKWebview的container content height如何调整到webview的内容?

标签 ios swift webview height wkwebview

我正在尝试获得我的 WKWebview 的正确高度和宽度内容,将 webview 容器高度设置为 webview 内容。 我已经尝试了很多关于堆栈溢出的解决方案。

使用这段代码,容器具有正确的高度,但是 webview 的宽度太大(好像 webview 被缩放了一样)

override func viewDidLoad() {

    let webConfiguration = WKWebViewConfiguration()
    let customFrame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 0.0, height: self.viewForWebview.frame.size.height))
    self.webView = WKWebView (frame: customFrame , configuration: webConfiguration)
    webView.translatesAutoresizingMaskIntoConstraints = false
    self.viewForWebview.addSubview(webView)

    NSLayoutConstraint.activate([
         self.webView.topAnchor.constraint(equalTo: self.viewForWebview.topAnchor),
        self.webView.bottomAnchor.constraint(equalTo: self.viewForWebview.bottomAnchor),
        self.webView.leadingAnchor.constraint(equalTo: self.viewForWebview.leadingAnchor),
        self.webView.trailingAnchor.constraint(equalTo: self.viewForWebview.trailingAnchor)
        ])

    webView.uiDelegate = self
     webView.navigationDelegate = self
     webView.scrollView.isScrollEnabled = false

    let bodyHtml = "<html><head><meta name=\"viewport\" content=\"initial-scale=1.0\" />" + htmlString + "</body></html>";  
    webView.loadHTMLString(bodyHtml, baseURL: Bundle.main.resourceURL)
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

    self.heightWebviewConstraint.constant = self.webView.scrollView.contentSize.height
    self.view.setNeedsLayout()
    self.view.setNeedsUpdateConstraints()
}

改为添加 <head>标签,我也尝试使用 evaluateJavaScript , 但在这种情况下,高度的容器太大,而 webview 的宽度太大。

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

    self.webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
        if complete != nil {
                self.webView.evaluateJavaScript("document.body.offsetHeight", completionHandler: { (height, error) in

                self.heightWebviewConstraint.constant = height as! CGFloat
                self.view.setNeedsLayout()
                self.view.setNeedsUpdateConstraints()
            })
        }

    })
}

我还尝试添加特定的 css 样式(没有 viewport 和没有 evaluateJavaScript ),在这种情况下,webview 的宽度没问题,但高度的容器太小(我的 webview 在顶部和底部被裁剪)。

let bodyHtml = "<html><head> <style>  img {  width:auto; height:auto;  max-width:100%; </style></head><body>" + htmlString + "</body></html>"

我迷失了所有这些解决方案。

最佳答案

我认为您实际上非常接近那里的解决方案。我遇到过类似的问题,并设法通过评估 javascript 来解决它。我们的实现有点不同,因为我们使用带有嵌套 WkWebViews 的堆栈 View 作为排列的 subview 。但我可以分享我们的代码(去除了堆栈 View 部分),您可以自己尝试一下。

    func addAndLoadWebView(with url: URL) {
         let wkWebView = WKWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))
         wkWebView.translatesAutoresizingMaskIntoConstraints = false
         wkWebView.navigationDelegate = self
         wkWebView.scrollView.isScrollEnabled = false
         wkWebView.scrollView.bounces = false
         wkWebView.scrollView.bouncesZoom = false
         wkWebView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
         self.view.addSubview(wkWebView)
         wkWebView.load(URLRequest(url: url))
    }

    @available(iOS 12.0, *)
    func asyncGetHTMLHeight(_ webview: WKWebView, completion: @escaping (CGFloat) -> Void) {
        webview.evaluateJavaScript("document.body.offsetHeight") { (result, _) in
            guard let result = result as? CGFloat else {
                completion(0.0)
                return
            }
            completion(result)
        }
    } 

    @available(iOS 12.0, *)
    func compatibleMeasureImageContent(with webview: WKWebView) {
    webview.evaluateJavaScript("document.body.firstChild.offsetHeight") { (result, _) in
        guard let result = result as? CGFloat else {
            return
        }
        var height = result
        webview.evaluateJavaScript("document.body.firstChild.width") { (result, _) in
            guard let width = result as? CGFloat else {
                return
            }
            // do something with width and height here
            // in our case images come 1 by one since our html is delivered in parts. You might have to check each image tag for its height for this calculation to work.
            }
        }
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
         self.asyncGetHTMLHeight(webView, completion: { newHeight in
             webView.frame.size.height = newHeight
             webView.removeConstraints(webView.constraints)
             webView.heightAnchor.constraint(equalToConstant: newHeight).isActive = true          
          })
    }

关于ios - WKWebview的container content height如何调整到webview的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54326329/

相关文章:

ios - 警告 ITMS-90176 - 无法识别的语言环境 base.lproj

ios - 检测手机屏幕是否在 iOS 中打开/关闭

Flutter:如何使 Web View 大小适合屏幕?

javascript - 如何使用数据库中的纯文本填充 android 中的 webview 并能够处理换行符

ios - 实时获取 Apple Watch 心率变异性 SDNN?

ios - iOS 7-10 收到通知时如何显示本地通知?

swift - 当我制作 2 个场景时,由于未捕获的异常而终止应用程序

ios - 如何得到类似苹果 map 状态栏的模糊效果?

Swift 枚举,从关联的枚举中获取原始值

java - android:不幸的是应用程序已停止错误