如果你想使用WKWebView,需要在程序中做一下判断,因为WKWebView只有在iOS8.0以后才出现的。
if([[[UIDevice currentDevice] systemVersion] integerValue] >= 8) {
WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 100, WIDTH, 0)];
[self.view addSubview:wkWebView];
wkWebView.navigationDelegate = self;
[wkWebView loadHTMLString:htmlStr baseURL:nil];
bottomView.frame = CGRectMake(0, CGRectGetMaxY(wkWebView.frame) + 10, WIDTH, 40);
} else {
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 100, WIDTH, 0)];
[self.view addSubview:webView];
webView.delegate = self;
[webView loadHTMLString:htmlStr baseURL:nil];
bottomView.frame = CGRectMake(0, CGRectGetMaxY(webView.frame) + 10, WIDTH, 40);
}
自适应高度的方法:
UIWebView 代理方法
#pragma UIWebView
-(void)webViewDidFinishLoad:(UIWebView*) webView {
CGFloat documentHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"content\").offsetHeight;"] floatValue];
CGRect frame = webView.frame;
frame.size.height = documentHeight + 10;
webView.frame = frame;
}
WKWebView 代理方法
#pragma WKWebView
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
[webView evaluateJavaScript:@"document.getElementById(\"content\").offsetHeight;" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
//获取页面高度,并重置webview的frame
CGFloat documentHeight = [result doubleValue];
CGRect frame = webView.frame;
frame.size.height = documentHeight + 10/*显示不全*/;
webView.frame = frame;
}