简体   繁体   English

使用UIWebView获取请求的网站的高度

[英]Get the height of a website requested with UIWebView

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. 当网站的实际大小远大于此值时,代码始终返回1000.0。 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. 我想显示WebView的孔内容,而无需在WebView中滚动。

You need to use the webViewDidFinishLoad delegate method, this answer was originally founded here How to determine the content size of a UIWebView? 您需要使用webViewDidFinishLoad委托方法,此答案最初是在这里建立的。 如何确定UIWebView的内容大小?

In your viewDidLoad method do something like this 在你的viewDidLoad方法中做这样的事情

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 希望这对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM