目錄
方法一:通過代理執行JS方法獲取
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[webView evaluateJavaScript:@"document.body.scrollHeight"
completionHandler:^(id _Nullable result,NSError *_Nullable error) {
// 高度
CGFloat scrollViewHeight = [result doubleValue];
}];
}
方法二:通過KVO獲取
// 添加KVO監聽
[_webView.scrollView addObserver:self
forKeyPath:@"contentSize"
options:NSKeyValueObservingOptionNew
context:nil];
// 實現監聽方法
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
context:(void *)context {
UIScrollView *scrollView = object;
NSLog(@"%@", @(scrollView.contentSize.height));
}
// 移除監聽
- (void)dealloc{
[self.webView.scrollView removeObserver:self
forKeyPath:@"contentSize"];
}