【问题标题】:Get the height of a website requested with UIWebView获取使用 UIWebView 请求的网站的高度
【发布时间】:2016-12-01 13:33:58
【问题描述】:

我请求使用此代码的网站

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

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

当网站的实际大小远大于此时,代码总是返回 1000.0。有没有办法得到真正的尺寸?我想显示 WebView 的孔内容而不需要在 WebView 内滚动。

【问题讨论】:

  • 你的网站是用Bootstrap开发的吗?

标签: ios swift webview uiwebview


【解决方案1】:

你需要使用webViewDidFinishLoad委托方法,这个答案最初建立在这里How to determine the content size of a UIWebView?

在你的 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()
}

希望对你有所帮助,问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 2020-11-08
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    相关资源
    最近更新 更多