方法调用顺序为从上至下,第二个方法会调用多次,这样才能确保在加载完毕网页数据后正确的计算出网页主体部分的高度
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[self setWebViewHtmlImageFitPhone];
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.clientHeight"] floatValue];
// //防止死循环
if (height != _viewModel.htmlHeight) {
_viewModel.htmlHeight = height;
if (_viewModel.htmlHeight > 0) {
NSLog(@"页面高度为:%ld",_viewModel.cellHeight);
}
}
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
if ([keyPath isEqualToString:@"contentSize"]) {
CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.clientHeight"] floatValue];
// //防止死循环
if (height != _viewModel.htmlHeight) {
// _viewModel.otherHeight = 533;
_viewModel.htmlHeight = height;
if (_viewModel.htmlHeight > 0) {
NSLog(@"页面高度为:%ld",_viewModel.cellHeight);
}
}
}
}