參考文檔:
iOS【終極方案】精準(zhǔn)獲取webView內(nèi)容高度席爽,自適應(yīng)高度
iOS精準(zhǔn)獲取webView內(nèi)容高度,自適應(yīng)高度(cell篇)
#pragma mark-懶加載webView
-(UIWebView *)webView{
if (!_webView) {
// _webView = [[UIWebView alloc] init];
// 1隨便多少,不能為0
_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,1)];
_webView.delegate = self;
_webView.scrollView.scrollEnabled = NO;
//這個(gè)屬性不加,webview會(huì)顯示很大.
_webView.scalesPageToFit = YES;
_webView.opaque =NO;
//預(yù)先加載url
[_webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/api/product/piclist?item_id=%@",SERVER,self.model.itemid]]]];
//給webView的scrollView的contentSize屬性添加監(jiān)聽(tīng),每當(dāng)內(nèi)容發(fā)生變化记靡,contentSize一定會(huì)跟著變,捕獲這個(gè)變動(dòng)
// [self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:nil];
[self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
}
return _webView;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.webView];
}
#pragma mark - UIWebView Delegate Methods
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[[MBProgressHUD HUDForView:self.view] removeFromSuperview];
// //獲取到webview的高度
//// CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"]floatValue];
// self.webViewHeight = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
// CGSize webSize = [webView sizeThatFits:CGSizeZero];
// [webView mas_updateConstraints:^(MASConstraintMaker *make) {
// make.height.equalTo(@(webSize.height));
// }];
// cellhight = self.webViewHeight;
// [self.tableView reloadData];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"webViewDidStartLoad");
// [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nonnull NSError *)error
{
NSLog(@"didFailLoadWithError===%@", error);
isOpen = !isOpen;
[[MBProgressHUD HUDForView:self.view] removeFromSuperview];
[AMShowMessage showMessage:@"加載失敗" withDuration:2];
[self.tableView reloadData];
}
#pragma mark-監(jiān)聽(tīng)的處理1团驱、監(jiān)聽(tīng)tableview偏移(控制導(dǎo)航的透明度)2摸吠、監(jiān)聽(tīng)webView.scrollView的contentSize的變化
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"contentOffset"])
{
UIButton * bottom = [self.view viewWithTag:1000];
CGPoint offset = [change[NSKeyValueChangeNewKey] CGPointValue];
CGFloat alpha = offset.y/NAVIGATION_BAR_HEIGHT >1.0f ? 1:offset.y/NAVIGATION_BAR_HEIGHT;
//設(shè)置一個(gè)顏色并轉(zhuǎn)化為圖片
UIImage *image = [self imageWithColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:alpha]];
self.navigationView.image = image;
self.navigationController.navigationBar.alpha = alpha;
self.topView.backgroundColor = [UIColor colorWithPatternImage:image];
self.topView.alpha = alpha;
if (offset.y > 0) {
self.navigationView.isAlphZero = NO;
bottom.hidden = NO;
}else{
self.navigationView.isAlphZero = YES;
bottom.hidden = YES;
}
}
if ([keyPath isEqualToString:@"contentSize"]) {
//通過(guò)JS代碼獲取webview的內(nèi)容高度
//self.webViewHeight = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
//通過(guò)webview的contentSize獲取內(nèi)容高度
//self.webViewHeight = [self.showWebView.scrollView contentSize].height;
CGRect frame = self.webView.frame;
//通過(guò)webview的contentSize獲取內(nèi)容高度
frame.size.height = self.webView.scrollView.contentSize.height;
//這里獲取webView的高度
_webViewHeight = frame.size.height;
NSLog(@"%f",frame.size.height);
cellhight =_webViewHeight;
CGSize webSize = [self.webView sizeThatFits:CGSizeZero];
[self.webView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@(webSize.height));
}];
[self.tableView reloadData];
}
}
反思:
1、在webViewDidFinishLoad方法中獲取webView的內(nèi)容高度嚎花,會(huì)導(dǎo)致獲取的內(nèi)容高度不精確寸痢,因?yàn)閣ebViewDidFinishLoad代理方法被調(diào)用時(shí),頁(yè)面并不一定完全展現(xiàn)完成紊选,可能有圖片還未加載出來(lái)啼止,導(dǎo)致此時(shí)獲取的高度是并不是最終高度,過(guò)會(huì)兒圖片加載出來(lái)后兵罢,瀏覽器會(huì)重新排版献烦,而我們?cè)谶@之前給了一個(gè)錯(cuò)誤高度,導(dǎo)致顯示異常卖词。所以需要在監(jiān)聽(tīng)到webView.scrollView的contentSize變化時(shí)仿荆,獲取到的webView的內(nèi)容高度才時(shí)精確的。
#pragma mark - UIWebView Delegate Methods
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[[MBProgressHUD HUDForView:self.view] removeFromSuperview];
// //獲取到webview的高度
//// CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"]floatValue];
// self.webViewHeight = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
// CGSize webSize = [webView sizeThatFits:CGSizeZero];
// [webView mas_updateConstraints:^(MASConstraintMaker *make) {
// make.height.equalTo(@(webSize.height));
// }];
// cellhight = self.webViewHeight;
[self.tableView reloadData];
}
2坏平、實(shí)例化創(chuàng)建webView的時(shí)候,必須設(shè)置frame值锦亦,否值不顯示
// 高度隨便多少,可以為1舶替,但不能為0,為0 則webView不顯示
_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,1)];