Perfect solution-adaptive content height for iOS WebView, ioswebview
/// // Initialization, self. view is the parent control ///_ webView = [[UIWebView alloc] initWithFrame: CGRectMake (0, 0, self. view. frame. size. width, 0)]; _ webView. delegate = self; _ webView. scrollView. bounces = NO; _ webView. scrollView. showsHorizontalScrollIndicator = NO; _ webView. scrollView. scrollEnabled = NO; [_ webView sizeToFit];
//////////////////////////// Set the content. Here, a div is encapsulated, used to obtain the actual height (pixel) of the content. htmlcontent is an html string // NSString * htmlcontent = [NSString stringWithFormat: @ "<div id = \" webview_content_wrapper \ ">%@ </div>", htmlcontent]; [_ webView loadHTMLString: htmlcontent baseURL: nil];
//// // Method overload of delegate //// //////////////////////////////////////// -(void) webViewDidFinishLoad :( UIWebView *) webView {// obtain the page height (pixel) NSString * clientheight_str = [webView stringByEvaluatingJavaScriptFromString: @ "document. body. offsetHeight "]; float clientheight = [clientheight_str floatValue]; // set it to WebView. frame = CGRectMake (0, 0, self. view. frame. size. width, clientheight); // obtain the optimal WebView size (point) CGSize frame = [webView sizeThatFits: webView. frame. size]; // obtain the actual height (pixel) of the content NSString * height_str = [webView stringByEvaluatingJavaScriptFromString: @ "document. getElementById ('webview _ content_wrapper '). offsetHeight + parseInt (window. getComputedStyle (document. getElementsByTagName ('body') [0]). getPropertyValue ('margin-top') + parseInt (window. getComputedStyle (document. getElementsByTagName ('body') [0]). getPropertyValue ('margin-bottom ') "]; float height = [height_str floatValue]; // actual content height (pixel) * Ratio of vertices to pixels height = height * frame. height/clientheight; // set the WebView height (point) again. frame = CGRectMake (0, 0, self. view. frame. size. width, height );}