saravanar
saravanar

Reputation: 649

WKWebView disable zoom and edit in swift

Working with WKWebView loading url in that web view can't able to disable the zoom and edit. Here my code.

 [myWebView = WKWebView(frame: CGRect( x: 0, y: 20, width: self.view.frame.width, height: self.view.frame.height - 20 ), configuration: WKWebViewConfiguration() )
        myWebView.translatesAutoresizingMaskIntoConstraints = false
        myWebView.navigationDelegate = self
        myWebView.uiDelegate = self
        self.myWebView.isMultipleTouchEnabled = false
        view = myWebView
        myWebView.scrollView.showsHorizontalScrollIndicator = false;
        myWebView.scrollView.showsVerticalScrollIndicator = false;

        myWebView.uiDelegate = self
        myWebView.navigationDelegate = self
        myWebView.scrollView.delegate = self


        myWebView.scrollView.bounces = false
        myWebView.scrollView.bouncesZoom = true
        myWebView.isMultipleTouchEnabled = false

      func viewForZooming(in: UIScrollView) -> UIView? {
        return nil;
      }

enter image description here

enter image description here

How can remove the edit and zoom open. help me thanks advance.

Upvotes: 2

Views: 9965

Answers (4)

Ely
Ely

Reputation: 9141

One line of code may be enough to disable zooming, editing and more, such as clicking links:

webView.isUserInteractionEnabled = false

Upvotes: 0

MingMan
MingMan

Reputation: 920

So now that

meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';

doesn't work anymore on many browsers (including Chrome and Safari) there is a new fix

Implement this in your view controller where "webView" is your webView. Make your View Controller inherit UIScrollViewDelegate

webView.load(URLRequest(url: url))
webView.scrollView.isMultipleTouchEnabled = false
webView.scrollView.delegate = self;

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

func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
   webView.scrollView.setZoomScale(1.0, animated: false);
   webView.scrollView.setContentOffset(CGPoint(x: 0,y: 0), animated: false);
}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    scrollView.panGestureRecognizer.isEnabled = false
}

Upvotes: 0

Ahmed Lotfy
Ahmed Lotfy

Reputation: 3906

Swift 4: Disable Zooming

override func viewDidLoad() {
      super.viewDidLoad()
      self.webView.scrollView.delegate = self
}

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

Upvotes: 7

Ganesh Manickam
Ganesh Manickam

Reputation: 2139

To disable zoom use below lines of code

      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)

Hope that will help you

Upvotes: 7

Related Questions