How to determine the content size of a UIWebView?

后端 未结 15 2126
灰色年华
灰色年华 2020-11-22 17:06

I have a UIWebView with different (single page) content. I\'d like to find out the CGSize of the content to resize my parent views appropriately. T

15条回答
  •  旧时难觅i
    2020-11-22 17:38

    Resurrecting this question because I found Ortwin's answer to only work MOST of the time...

    The webViewDidFinishLoad method may be called more than once, and the first value returned by sizeThatFits is only some portion of what the final size should be. Then for whatever reason the next call to sizeThatFits when webViewDidFinishLoad fires again will incorrectly return the same value it did before! This will happen randomly for the same content as if it's some kind of concurrency problem. Maybe this behaviour has changed over time, because I'm building for iOS 5 and have also found that sizeToFit works in much the same way (although previously this didn't?)

    I have settled on this simple solution:

    - (void)webViewDidFinishLoad:(UIWebView *)aWebView
    {        
        CGFloat height = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
        CGFloat width = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.width"] floatValue];
        CGRect frame = aWebView.frame;
        frame.size.height = height;
        frame.size.width = width;
        aWebView.frame = frame;
    }
    

    Swift (2.2):

    func webViewDidFinishLoad(webView: UIWebView) {
    
        if let heightString = webView.stringByEvaluatingJavaScriptFromString("document.height"),
            widthString = webView.stringByEvaluatingJavaScriptFromString("document.width"),
            height = Float(heightString),
            width = Float(widthString) {
    
            var rect = webView.frame
            rect.size.height = CGFloat(height)
            rect.size.width = CGFloat(width)
            webView.frame = rect
        }
    }
    

    Update: I have found as mentioned in the comments this doesn't seem to catch the case where the content has shrunk. Not sure if it's true for all content and OS version, give it a try.

提交回复
热议问题