How to determine the content size of a UIWebView?

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

    I'm also stuck on this problem, then I realized that if I want to calculate the dynamic height of the webView, I need to tell the width of the webView first, so I add one line before js and it turns out I can get very accurate actual height.

    The code is simple like this:

    -(void)webViewDidFinishLoad:(UIWebView *)webView
    {
    
        //tell the width first
        webView.width = [UIScreen mainScreen].bounds.size.width;
    
        //use js to get height dynamically
        CGFloat scrollSizeHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
        webView.height = scrollSizeHeight;
    
        webView.x = 0;
        webView.y = 0;
    
        //......
    }
    

提交回复
热议问题