更新WKWebView的高度

问题描述 投票:0回答:1

我需要帮助根据 WKWebKitView 内容高度更新 UITableViewCell 的高度。

我已经编写了基于

document.body.offsetHeight
获取高度的代码,但它不起作用,因为它在 webView 的末尾添加了空白空间。

所以我使用了以下方法。

extension PDDescriptionCell: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        self.summaryHeight?.constant = 0

        self.contentView.layoutIfNeeded()
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.summaryHeight?.constant = webView.scrollView.contentSize.height

        if let tableView = self.superview as? UITableView {
            tableView.beginUpdates()
            tableView.endUpdates()
        }

        self.contentView.layoutIfNeeded()
    }
}

问题是 PDDescriptionCell 是具有其他 tableViewCells 的 ProductDetail UITableViewController 的一部分,并且此 PDDescriptionCell 位于第 3 行。一旦我打开 ProductDetail 并滚动到 PDDescriptionCell 的第 3 行,高度就与第 3 行相同在xib文件中设置。但是,tableView 已更新。但 PDDescriptionCell 的正确高度不会更新。如果我向上滚动到第 1 行,然后返回到第 3 行,则会更新 PDDescriptionCell 的正确高度。

当第 3 行位于屏幕本身上时,如何确保通过这种查找高度的方式更新 PDDescriptionCell 的第 3 行的高度?

PDDescriptionCell 的完整代码:

class PDDescriptionCell: UITableViewCell {
    var product: ProductModel? {
        didSet {
            if let product = product {
                print(product.summary)

                loadContent(product.summary)
            }
        }
    }

    IBOutlet weak var summaryView: WKWebView!
    IBOutlet weak var summaryHeight: NSLayoutConstraint!

    private let configuration = WKWebViewConfiguration()

    override func awakeFromNib() {
        super.awakeFromNib()

        print("awake from nib")

        contentView.backgroundColor = .background1

        summaryView.navigationDelegate = self

        let preference = WKWebpagePreferences()

        preference.preferredContentMode = .mobile
        preference.allowsContentJavaScript = true

        let configuration = WKWebViewConfiguration()
        configuration.defaultWebpagePreferences = preference

        if let product = product {
            _ = WKWebViewConfiguration()

            print("HTML string is loaded")

            self.summaryView.loadHTMLString(product.summary, baseURL: nil)
            self.loadContent(product.summary)
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    private func loadContent(_ content: String) {
        let styledHTML = """
    <html>
    <head>
    <style> body { font-size: 30px; /\* Change the font size for the body as needed \*/ font-family: -apple-system; /\* Use the system font \*/ } table { width: 100%; border-collapse: collapse; } th, td { border: 1px solid #dddddd; text-align: left; padding: 8px; font-size: 20px; /\* Change the font size for table cells as needed \*/ } th { background-color: #f2f2f2; } img { width: 100%; /\* Set the width of images to 100% \*/ height: auto; /\* Set the height of images to auto to maintain aspect ratio \*/ margin-bottom: 50px; /\* Add space below each image \*/ } .semibold { font-weight: 600; /\* Set font weight to semibold \*/ } </style>
    <script type="text/javascript"> window.onload = function() { var body = document.body; var html = document.documentElement; var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight); window.webkit.messageHandlers.iOS.postMessage(height); } </script>
    </head>
    <body>
    \(content)
    </body>
    </html>
    """

        // Load the styled HTML content into the WKWebView
        self.summaryView.loadHTMLString(styledHTML, baseURL: nil)
    }
}

extension PDDescriptionCell: WKNavigationDelegate {
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        self.summaryHeight?.constant = 0

        self.contentView.layoutIfNeeded()
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.summaryHeight?.constant = webView.scrollView.contentSize.height

        if let tableView = self.superview as? UITableView {
            tableView.beginUpdates()
            tableView.endUpdates()
        }

        self.contentView.layoutIfNeeded()
    }
}

我尝试使用

evaluateJavaScript("document.readyState")
evaluateJavaScript("document.body.scrollHeight")
获取高度,但最终在网络视图的末尾添加了空白空间。我期望使用相同的代码来获取高度,因为它在更新高度时可以正常工作,但没有在适当的时间更新。

ios swift uitableview uikit wkwebview
1个回答
0
投票

如果网页视图解决方案不起作用,请尝试这种替代方法。

可以使用带有属性文本的标签来代替 webView。

  1. 用标签替换webView

  2. self.summaryView.loadHTMLString(styledHTML, baseURL: nil)
    替换为以下

    if let data = styledHTML.data(using: .utf8),
       let attributedText = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
        attributedTextLabel.attributedText = attributedText
    }
    
© www.soinside.com 2019 - 2024. All rights reserved.