//创建webView,显示html内容
UIWebView*webView = [[UIWebViewalloc]init];
[webViewloadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:self.cpDescUrl]]];
webView.scalesPageToFit=YES;
webView= webView;
webView.delegate=self;
//设置UIWebView高度等于html内容高度
//通过代理获取页面高度
- (void)webViewDidFinishLoad:(UIWebView*)webView {
//方法一:
CGFloat documentHeight= [[wb stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
//方法二:
CGRect frame = webView.frame;
frame.size.width=screenWith;
frame.size.height=1;//这步不能少,不然webView.scrollView.contentSize.height为零
webView.frame= frame;
frame.size.height= webView.scrollView.contentSize.height;
webView.frame= frame;
self.scrollView.contentSize=CGSizeMake(screenWith,self.contentHeight+frame.size.height+kMargin);
webView.scrollView.scrollEnabled=NO;
}
//原理:
方法一通过调用js来获取html的body内容高度。
方法二通过获取webview自适应设置的contentsize.height来获得内容高度,但要注意在获取正确contentsize.height时,webview的frame不能为CGRectZero。