How to determine the content size of a UIWebView?

后端 未结 15 1903
灰色年华
灰色年华 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条回答
  • 2020-11-22 17:32

    A simple solution would be to just use webView.scrollView.contentSize but I don't know if this works with JavaScript. If there is no JavaScript used this works for sure:

    - (void)webViewDidFinishLoad:(UIWebView *)aWebView {
        CGSize contentSize = aWebView.scrollView.contentSize;
        NSLog(@"webView contentSize: %@", NSStringFromCGSize(contentSize));
    }
    
    0 讨论(0)
  • 2020-11-22 17:33

    For iOS10, I was getting 0 (zero) value of document.height so document.body.scrollHeight is the solution to get height of document in Webview. The issue can be resolved also for width.

    0 讨论(0)
  • 2020-11-22 17:35

    In Xcode 8 and iOS 10 to determine the height of a web view. you can get height using

    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {
        CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
        NSLog(@"Webview height is:: %f", height);
    }
    

    OR for Swift

     func webViewDidFinishLoad(aWebView:UIWebView){
            let height: Float = (aWebView.stringByEvaluatingJavaScriptFromString("document.body.scrollHeight")?.toFloat())!
            print("Webview height is::\(height)")
        }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 17:39

    AFAIK you can use [webView sizeThatFits:CGSizeZero] to figure out it's content size.

    0 讨论(0)
  • 2020-11-22 17:42

    None of the suggestions here helped me with my situation, but I read something that did give me an answer. I have a ViewController with a fixed set of UI controls followed by a UIWebView. I wanted the entire page to scroll as though the UI controls were connected to the HTML content, so I disable scrolling on the UIWebView and must then set the content size of a parent scroll view correctly.

    The important tip turned out to be that UIWebView does not report its size correctly until rendered to the screen. So when I load the content I set the content size to the available screen height. Then, in viewDidAppear I update the scrollview's content size to the correct value. This worked for me because I am calling loadHTMLString on local content. If you are using loadRequest you may need to update the contentSize in webViewDidFinishLoad also, depending on how quickly the html is retrieved.

    There is no flickering, because only the invisible part of the scroll view is changed.

    0 讨论(0)
提交回复
热议问题