iOS WebView Adaptive Real content height 4 ways

Source: Internet
Author: User

Sometimes encounter similar hardship demand, webview adaptive actual content height below are four ways to use

Method 1: Get the contentsize of Scrovllview in WebView to set

123456 -(void)webviewdidfinishload:(uiwebview *)webView{ cgfloat webviewheight=[webView. ScrollView contentsize]. Height; cgrect newframe = webView. Frame; newframe. Size. Height = webviewheight; webView. Frame = newframe; }

Method 2: Execute the JS statement to get the DOM height of the HTML document directly

1234567 - (void) webviewdidfinishload: ( Uiwebview *) Webview{ cgfloat webviewheight= [[webView stringbyevaluatingjavascriptfromstring : @ "Document.body.offsetHeight"]floatvalue]; //CGFloat webviewheight= [[WebView stringbyevaluatingjavascriptfromstring: @ "Document.body.scrollHeight"] Floatvalue]; cgrect newframe = webView. Frame; newframe. Size. Height = webviewheight; webView. Frame = newframe; }

Method 3: First set the height of the UIWebView to minimum and then use Sizethatfits to return exactly the right size

123456 -(void)webviewdidfinishload:(uiwebview *)WebView { cgsize actualsize = [webView sizethatfits: Cgsizezero]; cgrect newframe = webView. Frame; newframe. Size. Height = actualsize. Height; webView. Frame = newframe; }

Method 4. Traverse the WebView sub-view to get the Uiwebdocumentview height i.e. the actual height

12345678910111213141516 -(void)webviewdidfinishload:(uiwebview *)webView{ cgfloat webviewheight = 0.0f; if ([webView. Subviews Count] > 0) { UIView *scrollerview = webView. Subviews[0]; if ([scrollerview. Subviews Count] > 0) { UIView *webdocview = scrollerview. Subviews. Lastobject; if ([webdocview iskindofclass:[nsclassfromstring(@ " Uiwebdocumentview ") class]) { webviewheight = webdocview. Frame. Size. Height; Get the height of a document webView. Frame= webdocview. Frame; //Update the height of the UIWebView } } }}

iOS WebView Adaptive Real content height 4 ways

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.