How to determine the content size of a UIWebView?

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

    This's weird!

    I tested the solutions both sizeThatFits: and [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] are NOT working for me.

    However, I found an interesting easy way to get the right height of webpage content. Currently, I used it in my delegate method scrollViewDidScroll:.

    CGFloat contentHeight = scrollView.contentSize.height - CGRectGetHeight(scrollView.frame);
    

    Verified in iOS 9.3 Simulator/Device, good luck!

    EDIT:

    Background: The html content is calculated by my string variable and HTTP content template, loaded by method loadHTMLString:baseURL:, no registered JS scripts there.

提交回复
热议问题