今天使用wkwebview 但发现 内容超出了屏幕,于是找类似UIWebView 的 scalesPageToFit 效果的属性,发现没有,于是开始百度,最后找到一个方法,下面分享给大家,直接粘贴复制就OK了
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(10, 20, WIDTH - 20, HEIGHT - 20) configuration:wkWebConfig];
网友评论