Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable zoom in WKWebView?

Tags:

Does anyone know a nice simple way to disable double-click and pinch zooming in a WKWebView? Nothing I've tried works:

Webview.scrollView.allowsMagnification = false;    // Error: value of type WKWebView has no member allowsMagnification  Webview.scrollView.isMultipleTouchEnabled = false; // doesn't do anything 

In the html:

<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> // pile of crap, does nothing 
like image 624
Bazley Avatar asked Nov 06 '16 17:11

Bazley


People also ask

How do you stop a website from zooming in?

To disable the zooming option with the multi-touch gesture we can use surefox browser but still, a user can zoom in or out by double tapping on the screen. We can use the <meta> tag to disable zoom in and out on a mobile web page.

What is WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.


1 Answers

You will have to add maximum scale in script.

The following code should help you:

let source: String = "var meta = document.createElement('meta');" +     "meta.name = 'viewport';" +     "meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';" +     "var head = document.getElementsByTagName('head')[0];" +     "head.appendChild(meta);"  let script: WKUserScript = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true) let userContentController: WKUserContentController = WKUserContentController() let conf = WKWebViewConfiguration() conf.userContentController = userContentController userContentController.addUserScript(script) let webView = WKWebView(frame: CGRect.zero, configuration: conf) 
like image 194
pritam001 Avatar answered Sep 23 '22 21:09

pritam001