WKWebView *webView = [[WKWebView alloc] init];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"urlString"]]];
#pragma mark - WKNavigationDelegate
// 頁面加載完成之後調用 此方法會調用多次
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
__block CGFloat webViewHeight;
//獲取內容實際高度(像素)@"document.getElementById(\"content\").offsetHeight;"
[webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result,NSError * _Nullable error) {
// 此處js字符串採用scrollHeight而不是offsetHeight是因爲後者並獲取不到高度,看參考資料說是對於加載html字符串的情況下使用後者可以(@"document.getElementById(\"content\").offsetHeight;"),但如果是和我一樣直接加載原站內容使用前者更合適
//獲取頁面高度,並重置webview的frame
webViewHeight = [result doubleValue];
webView.height = webViewHeight;
_mainScrollView.contentSize = CGSizeMake(Screen_Width, webView.top + webViewHeight + 10);
NSLog(@"%f",webViewHeight);
}];
NSLog(@"結束加載");
}
WKWebView獲取內容高度
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.