Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView inside a tableView Cell dynamic height

I have a WKWebView inside a UITableViewCell, which loads an HTML string (I have embedded a tweet).

I have tried all types of solutions on Stack Overflow, but they didn't work for me. Here is an example of something I tried:

class DetailWebCell: UITableViewCell {


    @IBOutlet weak var detailWebView: WKWebView!

    @IBOutlet weak var containerHeight: NSLayoutConstraint!

    override func awakeFromNib() {
        super.awakeFromNib()
        detailWebView.scrollView.isScrollEnabled = false

    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.detailWebView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
            if complete != nil {
                self.detailWebView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
                    self.containerHeight.constant = height as! CGFloat
                })
            }

            })
    }


}

But the cell's height remains the same

image

I want the height of the WKWebview to be the same height as the tableView Cell's

like image 888
H. Osjir Avatar asked Oct 21 '19 07:10

H. Osjir


1 Answers

try this

self.containerHeight.constant = detailWebView.scrollView.contentSize.height
like image 121
Nikunj Kumbhani Avatar answered Sep 18 '22 00:09

Nikunj Kumbhani