how to disable Zoom-Out & Zoom-In

Hi developers is there any chanse to disable Zoom-Out & Zoom-In in WkWebView?

here's all my code so far keep in mind i'm only displaying a web in webView i don't have any other functions 😕




import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {
    @IBOutlet var webView: WKWebView!
  
    override func viewDidLoad() {
        super.viewDidLoad()
        displayWebPage()
        let preferences = WKPreferences()
        preferences.javaScriptEnabled = true
        let configuration = WKWebViewConfiguration()
        configuration.preferences = preferences
        let webView = WKWebView(frame: .zero, configuration: configuration)
        // Do any additional setup after loading the view, typically from a nib.
      
    }
    private func displayWebPage() {
      
        let url = URL(string: "hee hee")
        let request = URLRequest(url: url!)
        webView.navigationDelegate = self
        webView.load(request)
        webView.sizeToFit()
    }
  
  
  
  
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print(error)
    }
}

Did you try adding in viewDidload

webView.scrollView.minimumZoomScale = 1.0

webView.scrollView.maximumZoomScale = 1.0

thaks for answer 🙂

webView keeps zooming out and in same as before nothing change 😟

I'm not sure, but you could try:



To dispable pinch gesture on the view, set

webView.scrollView.multipleTouchEnabled = false



and / or

add the UIScrollViewDelegate func



func viewForZooming(in scrollView: UIScrollView) -> UIView? {

return nil

}


Should set delegate for webView.scrollView to self.

Not working 😟

try this

webView.scrollView.delegate = self

public func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
        scrollView.pinchGestureRecognizer?.isEnabled = false
    }


it'll disable zooming by pinch. I'm looking how to disable the double-tap for it.

Hi all.. in my case, I need to setup the webview zooming function.. How to enable it? Is it auto set? I already to pinch zooming in emulator, nothing happened 😢

You Have to maintain in delegate Method. First You Have to implicate this Delegate, UIScrollViewDelegate. Then only its work



@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.webView.scrollView.delegate = self
}
//MARK: - UIScrollViewDelegate
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
scrollView.pinchGestureRecognizer?.isEnabled = false
}


I Think It may Useful.

For anyone still having this issue you can fix it by adding some viewport constraints to the head of the html of the page you have loaded. I do this with some javascript I add to the end of the document:

Code Block swift
    lazy var webView: WKWebView = {
        let configuration = WKWebViewConfiguration()
        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)
        configuration.userContentController.addUserScript(script)
        let webView = WKWebView(frame: .zero, configuration: configuration)
        webView.navigationDelegate = self
        return webView
    }()

Alternatively if you are loading in html yourself you can get the same result like this:
Code Block swift
 webView.loadHTMLString("<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0 maximum-scale=1.0 user-scalable=no\" charset=\"utf-8\" /></head><body>\(YOUR-HTML-CODE-HERE)</body></html>", baseURL: nil)


Hope this helps!
Thank you [vrijma] it works
how to disable Zoom-Out &amp; Zoom-In
 
 
Q