XDR
XDR

Reputation: 4470

Listening for when WKWebView is zoomed out on iPhone

How can I listen for when a WKWebView is zoomed out on an iPhone? (e.g., when a user pinches a web page and makes it smaller than 100% zoom)

WKWebView.magnification is only on macOS, not on iOS. See:

https://developer.apple.com/documentation/webkit/wkwebview/1414985-magnification

I would prefer solutions in Swift 4 or 4.2, as they're current, and I don't know older Swift or Objective-C.

Upvotes: 2

Views: 640

Answers (1)

Code Different
Code Different

Reputation: 93151

A WkWebView contains a scrollView which contains further subviews to display the webpage's contents. To listen in on zooming events, make your view controller conform to UIScrollViewDelegate:

class ViewController: UIViewController, UIScrollViewDelegate {
    @IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        webView.scrollView.delegate = self
        webView.load(URLRequest(url: URL(string: "https://www.apple.com")!))
    }

    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        print(scrollView.zoomScale)
    }
}

Upvotes: 2

Related Questions