How to determine the content size of a UIWebView?

后端 未结 15 1873
灰色年华
灰色年华 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:43

    Xcode8 swift3.1:

    1. Set webView height to 0 first.
    2. In webViewDidFinishLoad Delegate:

    let height = webView.scrollView.contentSize.height

    Without step1, if webview.height > actual contentHeight, step 2 will return webview.height but not contentsize.height.

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

    When using webview as a subview somewhere in scrollview, you can set height constraint to some constant value and later make outlet from it and use it like:

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        webView.scrollView.scrollEnabled = NO;
        _webViewHeight.constant = webView.scrollView.contentSize.height;
    }
    
    0 讨论(0)
  • 2020-11-22 17:46

    If your HTML contains heavy HTML-contents like iframe's (i.e. facebook-, twitter, instagram-embeds) the real solution is much more difficult, first wrap your HTML:

    [htmlContent appendFormat:@"<html>", [[LocalizationStore instance] currentTextDir], [[LocalizationStore instance] currentLang]];
    [htmlContent appendFormat:@"<head>"];
    [htmlContent appendString:@"<script type=\"text/javascript\">"];
    [htmlContent appendFormat:@"    var lastHeight = 0;"];
    [htmlContent appendFormat:@"    function updateHeight() { var h = document.getElementById('content').offsetHeight; if (lastHeight != h) { lastHeight = h; window.location.href = \"x-update-webview-height://\" + h } }"];
    [htmlContent appendFormat:@"    window.onload = function() {"];
    [htmlContent appendFormat:@"        setTimeout(updateHeight, 1000);"];
    [htmlContent appendFormat:@"        setTimeout(updateHeight, 3000);"];
    [htmlContent appendFormat:@"        if (window.intervalId) { clearInterval(window.intervalId); }"];
    [htmlContent appendFormat:@"        window.intervalId = setInterval(updateHeight, 5000);"];
    [htmlContent appendFormat:@"        setTimeout(function(){ clearInterval(window.intervalId); window.intervalId = null; }, 30000);"];
    [htmlContent appendFormat:@"    };"];
    [htmlContent appendFormat:@"</script>"];
    [htmlContent appendFormat:@"..."]; // Rest of your HTML <head>-section
    [htmlContent appendFormat:@"</head>"];
    [htmlContent appendFormat:@"<body>"];
    [htmlContent appendFormat:@"<div id=\"content\">"]; // !important https://stackoverflow.com/a/8031442/1046909
    [htmlContent appendFormat:@"..."]; // Your HTML-content
    [htmlContent appendFormat:@"</div>"]; // </div id="content">
    [htmlContent appendFormat:@"</body>"];
    [htmlContent appendFormat:@"</html>"];
    

    Then add handling x-update-webview-height-scheme into your shouldStartLoadWithRequest:

        if (navigationType == UIWebViewNavigationTypeLinkClicked || navigationType == UIWebViewNavigationTypeOther) {
        // Handling Custom URL Scheme
        if([[[request URL] scheme] isEqualToString:@"x-update-webview-height"]) {
            NSInteger currentWebViewHeight = [[[request URL] host] intValue];
            if (_lastWebViewHeight != currentWebViewHeight) {
                _lastWebViewHeight = currentWebViewHeight; // class property
                _realWebViewHeight = currentWebViewHeight; // class property
                [self layoutSubviews];
            }
            return NO;
        }
        ...
    

    And finally add the following code inside your layoutSubviews:

        ...
        NSInteger webViewHeight = 0;
    
        if (_realWebViewHeight > 0) {
            webViewHeight = _realWebViewHeight;
            _realWebViewHeight = 0;
        } else {
            webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"] integerValue];
        }
    
        upateWebViewHeightTheWayYorLike(webViewHeight);// Now your have real WebViewHeight so you can update your webview height you like.
        ...
    

    P.S. You can implement time delaying (SetTimeout and setInterval) inside your ObjectiveC/Swift-code - it's up to you.

    P.S.S. Important info about UIWebView and Facebook Embeds: Embedded Facebook post does not shows properly in UIWebView

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