Ios webview adaptive content height four methods
// Method 1
-(Void) webViewDidFinishLoad :( UIWebView *) webView
{
CGFloat webViewHeight = [webView. scrollView contentSize]. height;
CGRect newFrame = webView. frame;
NewFrame. size. height = webViewHeight;
WebView. frame = newFrame;
_ WebTablewView. contentSize = CGSizeMake (320, newFrame. size. height + 64 + KWIDTH-100 );
}
// 2. Execute the js statement to directly obtain the dom height of the html document
-(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. First set the height of UIWebView to the minimum, and then use sizeThatFits to return a proper 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 subview to obtain the UIWebDocumentView height, that is, 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 ([webDocView isKindOfClass: [NSClassFromString (@ UIWebDocumentView) class])
{
WebViewHeight = webDocView. frame. size. height; // obtain the document height.
WebView. frame = webDocView. frame;
// Update the height of the UIWebView
}
}
}
}