Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何调整WKWebview';容器内容相对于webview内容的高度?_Ios_Swift_Webview_Height_Wkwebview - Fatal编程技术网

Ios 如何调整WKWebview';容器内容相对于webview内容的高度?

Ios 如何调整WKWebview';容器内容相对于webview内容的高度?,ios,swift,webview,height,wkwebview,Ios,Swift,Webview,Height,Wkwebview,我正在尝试获取我的WKWebview内容的正确高度和宽度,以将webview容器高度设置为webview内容。 我已经尝试了很多解决堆栈溢出的方法 使用此代码,容器具有正确的高度,但webview的宽度太大(好像webview被缩放) 我还尝试添加特定的css样式(没有视口和evaluateJavaScript),在这种情况下,webview的宽度可以,但高度的容器太小(我的webview在顶部和底部被裁剪) let bodyHtml=“img{宽度:自动;高度:自动;最大宽度:100%;”+h

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

使用此代码,容器具有正确的高度,但webview的宽度太大(好像webview被缩放)

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

let bodyHtml=“img{宽度:自动;高度:自动;最大宽度:100%;”+htmlString+“”

我对所有这些解决方案都迷惑不解。

我认为您实际上离解决方案很近。我曾经遇到过类似的问题,并设法通过评估javascript解决了它。我们的实现有点不同,因为我们使用带有嵌套WKWebView的stackview作为排列子视图。但我可以分享我们的代码(去掉stackview部件)您可以自己尝试

    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          
          })
    }

不适用于我,新结果:高度的容器太大,web视图的宽度太小:(你是在严格加载html还是PDF?意识到我们实际上在代码注入之前添加了一些html规范化。不确定它是否有帮助,但也可以将其链接起来。html仅使用html图像中的标记有点棘手,你可能需要编写一些javascript来检查每个图像标记。实际上,我们对图像的高度计算是不同的。
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()
            })
        }

    })
}
let bodyHtml = "<html><head> <style>  img {  width:auto; height:auto;  max-width:100%; </style></head><body>" + htmlString + "</body></html>"
    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          
          })
    }