表格中有WKWebView底部有空白。
理论上等网页加载完成,在didFinishNavigation修改表格高度,但是发现算出的高度比实际高出现底部空白。
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigatio{ // NSString *urlString =[webView.URL absoluteString]; // self.model.contentHeight = webView.scrollView.contentSize.height; // if(self.model && [self.model isKindOfClass:[XQBQuestionDetailAnswerListUnitEntity class]] && self.model.contentHeight < webView.scrollView.contentSize.height && !self.model.isDidFinishNavigationMaxHeight) // { // if([PPSingleObject sharedInstance].isInQuestionDetailViewController) // { // self.model.isDidFinishNavigationMaxHeight = YES; // self.model.contentHeight = webView.scrollView.contentSize.height; // self.model.totalHeight = self.model.contentHeight + self.model.otherTotalHeight; // if(self.changeWebBlock && self.indexPath) // { // self.changeWebBlock(self.indexPath); // } // } // } }
解决方案:kvo监控self.webView.scrollView的contentSize,延迟修正表格高度,只修正一次,防止循环刷页面。这样也存在不友好的问题,页面刷新两边产生闪烁,当有大图片或多个视频或图片时显示不完全。所以显示纯文本类的h5很有效,含单张不大的图片或含单个视频也可以。
@weakify(self); [RACObserve(self.webView.scrollView, contentSize) subscribeNext:^(id x) { @strongify(self); // NSLog(@"xxxxxx:%@, nowTime:%lld毫秒 self.webView.scrollView.contentSize.height:%f,self.model.Body:%@", x, (long long)([[NSDate date] timeIntervalSince1970]*1000), self.webView.scrollView.contentSize.height,self.model.Body); // CGFloat contentHeight = self.webView.scrollView.contentSize.height; if(self.model && [self.model isKindOfClass:[XQBQuestionDetailAnswerListUnitEntity class]] && self.model.contentHeight < self.webView.scrollView.contentSize.height && !self.model.isMaxHeight && [PPSingleObject sharedInstance].isInQuestionDetailViewController && self.indexPath) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ @strongify(self); if(self.model && [self.model isKindOfClass:[XQBQuestionDetailAnswerListUnitEntity class]] && self.model.contentHeight < self.webView.scrollView.contentSize.height && !self.model.isMaxHeight && [PPSingleObject sharedInstance].isInQuestionDetailViewController && self.indexPath) { bg_dispatch_main_async_safe(^{ @strongify(self); if(self.model && [self.model isKindOfClass:[XQBQuestionDetailAnswerListUnitEntity class]] && self.model.contentHeight < self.webView.scrollView.contentSize.height && !self.model.isMaxHeight && [PPSingleObject sharedInstance].isInQuestionDetailViewController && self.indexPath) { self.model.isMaxHeight = YES; self.model.contentHeight = self.webView.scrollView.contentSize.height; self.model.totalHeight = self.model.contentHeight + self.model.otherTotalHeight; if(self.changeWebBlock && self.indexPath) { self.changeWebBlock(self.indexPath); } } }); } }); } }];