WKWebView相当于UIWebView的scalesPageToFit

我正在更新我的iOS应用程序以用WKWebViewreplaceUIWebView 。 但是,我不明白如何实现与WKWebView相同的行为。 使用UIWebView我使用了scalesPageToFit以确保网页pag显示与屏幕大小相同的大小(以便全屏显示而不滚动)。

我在网上发现了这个解决scheme,但它不起作用:

 - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation { NSString *javascript = @"var meta = document.createElement('meta');meta.setAttribute('name', 'viewport');meta.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no');document.getElementsByTagName('head')[0].appendChild(meta);"; [webView evaluateJavaScript:javascript completionHandler:nil]; } 

4 Solutions collect form web for “WKWebView相当于UIWebView的scalesPageToFit”

你也可以试试WKUserScript。

这是我的工作configuration:

 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; wkWebV = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig]; 

你可以添加额外的configuration到你的WKWebViewConfiguration。

回复nferocious76的回复,在swift代码中:Swift2.x版本

 let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);" let userScript = WKUserScript(source: jscript, injectionTime: WKUserScriptInjectionTime.AtDocumentEnd, forMainFrameOnly: true) let wkUController = WKUserContentController() wkUController.addUserScript(userScript) let wkWebConfig = WKWebViewConfiguration() wkWebConfig.userContentController = wkUController let youWebView = WKWebView(frame: CGRectZero, configuration: wkWebConfig) 

swift3版本

 let jscript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);" let userScript = WKUserScript(source: jscript, injectionTime: .atDocumentEnd, forMainFrameOnly: true) let wkUController = WKUserContentController() wkUController.addUserScript(userScript) let wkWebConfig = WKWebViewConfiguration() wkWebConfig.userContentController = wkUController let yourWebView = WKWebView(frame: self.view.bounds, configuration: wkWebConfig) 

类似于@ nferocious76的,但在Swift语言

 var scriptContent = "var meta = document.createElement('meta');" scriptContent += "meta.name='viewport';" scriptContent += "meta.content='width=device-width';" scriptContent += "document.getElementsByTagName('head')[0].appendChild(meta);" webView.evaluateJavaScript(scriptContent, completionHandler: nil) 

C#版本,用于Xamarin自定义渲染器:

 string jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"; WKUserScript wkUScript = new WKUserScript((NSString)jScript, WKUserScriptInjectionTime.AtDocumentEnd, true); WKUserContentController wkUController = new WKUserContentController(); wkUController.AddUserScript(wkUScript); WKWebViewConfiguration wkWebConfig = new WKWebViewConfiguration(); wkWebConfig.UserContentController = wkUController; WKWebView webView=new WKWebView(Frame, wkWebConfig);