How to disable zooming in a UIWebView in Swift?

To disable zooming in a UIWebView in Swift, you can use the UIScrollViewDelegate method viewForZooming(in:) to return nil, effectively disabling zooming. Here’s an example code snippet:

class ViewController: UIViewController, UIWebViewDelegate, UIScrollViewDelegate {
    
    @IBOutlet weak var webView: UIWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        webView.delegate = self
        webView.scrollView.delegate = self
        webView.scalesPageToFit = true
        
        if let url = URL(string: "https://www.example.com") {
            let request = URLRequest(url: url)
            webView.loadRequest(request)
        }
    }
    
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return nil
    }
    
}

In this code snippet, the viewDidLoad method sets the delegate property of the UIWebView to self, and the scrollView.delegate property to self as well, allowing us to implement the UIScrollViewDelegate method viewForZooming(in:). We also set scalesPageToFit to true to ensure that the web content fits the view’s bounds. Finally, we load a URL into the web view using a URLRequest.

The viewForZooming(in:) method simply returns nil, which disables zooming in the web view.

A pat on the back !!