ios-WKWebView:自适应屏幕

WKWebView自适应屏幕用到了WKWebView加载js的特性

代码如下

WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];

WKUserContentController *wkUController = [[WKUserContentController alloc] init];

wkWebConfig.userContentController = wkUController;

//自适应屏幕的宽度js

NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";

WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];

//添加js调用

[wkUController addUserScript:wkUserScript];

self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1) configuration:wkWebConfig];

jSString字符串对应是要加载的js方法,这个有学过js很好理解。js的方法在加载钱最好在html测试几番!!

你可能感兴趣的:(ios-WKWebView:自适应屏幕)