遵循UIWebViewDelegate协议,在代理方法里获取内容整体高度,调整 webview的frame
NSString *htmlStr = "<p><img src=\"https://img20.360buyimg.com/vc/jfs/t1/37127/23/887/799049/5cadd35dE9ec3cc24/766b05c2232b3b23.jpg\" style=\"max-width:100%;\"><br></p><p><img src=\"https://img20.360buyimg.com/vc/jfs/t1/36403/9/5981/4667392/5cc17b58E75c1b2d8/19f52d37e5813053.jpg\" style=\"max-width:100%;\"><br></p>";
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(kScaleX * 15, 0, kScaleX * 345, 10)];
webView.delegate = self;
webView.userInteractionEnabled = NO;
[webView loadHTMLString:htmlStr baseURL:nil];
[_myScrollView addSubview:webView];
代理:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *contentH = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"];
webView.frame = CGRectMake(kScaleX * 15, 0, kScaleX * 345, contentH.floatValue);
_myScrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(webView.frame) + 20);
}