//創(chuàng)建webView,顯示html內(nèi)容
UIWebView*webView = [[UIWebViewalloc]init];
[webViewloadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:self.cpDescUrl]]];
webView.scalesPageToFit=YES;
webView= webView;
webView.delegate=self;
//設(shè)置UIWebView高度等于html內(nèi)容高度
//通過代理獲取頁面高度
- (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;
}
//原理:
方法一通過調(diào)用js來獲取html的body內(nèi)容高度默终。
方法二通過獲取webview自適應(yīng)設(shè)置的contentsize.height來獲得內(nèi)容高度犁罩,但要注意在獲取正確contentsize.height時,webview的frame不能為CGRectZero床估。