iOS WebView Adaptive Real content height 4 ways

Source: Internet
Author: User

The first of these methods

-(void) Webviewdidfinishload: (uiwebview *) WebView

{

cgfloat Webviewheight=[webview. ScrollViewcontentsize]. height;

cgrect newframe = WebView. frame;

Newframe. size. height = webviewheight;

WebView. frame = newframe;

_webtablewview. contentsize =cgsizemake(newframe. Size. height + + kwidth -+);


}


//2. Execute the JS statement to get the DOM height of the HTML document directly
-(void) webviewdidfinishload: ( UIWebView *) webview{

Cgfloatwebviewheight =[[webviewstringbyevaluatingjavascriptfromstring:@ "Document.body.offsetHeight"] Floatvalue];

CGFloat webviewheight= [[webviewstringbyevaluatingjavascriptfromstring:@ "Document.body.scrollHeight"] Floatvalue];

Cgrectnewframe = Webview.frame;

Newframe.size.height= Webviewheight;

Webview.frame= Newframe;

}

// Method 3. Set the height of the UIWebView to a minimum, and then use Sizethatfits to return exactly the right size

-(void) Webviewdidfinishload: (uiwebview*) webvie{

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

-(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 ([Webdocviewiskindofclass: [nsclassfromstring(@ "Uiwebdocume Ntview ")class])

{

Webviewheight = Webdocview. frame. size. height; // get the height of a document

WebView. frame=webdocview. frame;

// update The height of the UIWebView

}

}

}

}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS WebView Adaptive Real content height 4 ways

Related Article

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.