在webView
的webViewDidFinishLoad
中去处理
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//方法一:
//在计算UIWebView的高度时,由于webView的加载图片的延迟,会导致计算的高度不准确
CGFloat height=webView.scrollView.contentSize.height;
//方法二:
//观察者模式
[webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:@"webViewHeight"];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
//获取到webView.scrollView
UIScrollView *scrollView=object;
//当前变化后的高度
CGFloat height=scrollView.contentSize.height;
//webView
UIWebView *webView=(UIWebView *)scrollView.superview;
//字段context
NSString *value=[NSString stringWithFormat:@"%@",context];
if ([value isEqualToString:@"webViewHeight"]) {
}
}
网友评论