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