WKWebView equivalent for UIWebView's scalesPageToFit WKWebView equivalent for UIWebView's scalesPageToFit ios ios

WKWebView equivalent for UIWebView's scalesPageToFit


you can also try the WKUserScript.

here's my working config:

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];

you can add additional configuration to your WKWebViewConfiguration.


Echo of nferocious76's answer, in swift code:Swift2.x version

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 = wkUControllerlet youWebView = WKWebView(frame: CGRectZero, configuration: wkWebConfig)

swift3 version

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 = wkUControllerlet yourWebView = WKWebView(frame: self.view.bounds, configuration: wkWebConfig)


Similar to @nferocious76's but in Swift language

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)