不管是UIWebView还是WKWebView,都是key在webView初始化完成的时候添加KVO,用来监听webView的contentSize变化,添加KVO:
[webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
KVO的监听方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"contentSize"]) {
CGSize fittingSize = [_wvDetails sizeThatFits:CGSizeZero];
_wvDetails.frame = CGRectMake(0, 0, Screen_Width, fittingSize.height);
[_tvList reloadData];
}
}
在退出当前页面的时候,记得一定要移除监听,不然就崩
- (void)dealloc{
[_wvDetails.scrollView removeObserver:self forKeyPath:@"contentSize"];
}