0

I request a Website with this code

let requestObj = NSURLRequest(URL: url)
            myWebView.loadRequest(requestObj)

            print (myWebView.scrollView.contentSize.height) //1
            print (myWebView.frame.size.height)   //2

The code always return 1000.0 when the real size of the website is much more than that. Is there a way to get the real size ? I want to show the hole content of a WebView without the need of scrolling within the WebView.

user567
  • 3,712
  • 9
  • 47
  • 80

1 Answers1

0

You need to use the webViewDidFinishLoad delegate method, this answer was originally founded here How to determine the content size of a UIWebView?

In your viewDidLoad method do something like this

override func viewDidLoad() {
    super.viewDidLoad()
    self.webView.loadHTMLString(component.html, baseURL: nil)
    self.webView.scrollView.isScrollEnabled = false
    self.webView.scrollView.bounces = false
    self.webView.delegate = self
    self.delegate = delegate
}

public func webViewDidFinishLoad(_ webView: UIWebView)
    {
    var frame = webView.frame
    frame.size.height = 1
    webView.frame = frame

    let fittingSize = webView.sizeThatFits(CGSize(width: 0, height: 0))
    frame.size = fittingSize
    webView.frame = frame
    webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

    self.delegate?.heightHasChanged(indexPath:self.indexPath, newHeight: frame.height)
    self.loadingView.isHidden = true
    self.loadingActivityIndicator.stopAnimating()
}

I hope this helps you, best regards

Community
  • 1
  • 1
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55