DàChún
DàChún

Reputation: 5146

How to listen Zoom in/out event of WKWebView

I want to hide/show navigationBar of a UINavigationController when the WKWebView zooms out/in.

To hide or show a UINavigationBar is quite easy as follows:

self.navigationController?.navigationBarHidden = true

But the problem is that I don't know where to put the code.

I am thinking to intercept the zoom event of WKWebView. May be there are other ways, any comments are welcome.

Upvotes: 0

Views: 1309

Answers (2)

DàChún
DàChún

Reputation: 5146

I found another way to achieve it:

self.navigationController?.hidesBarsOnSwipe = true

iOS 8.0 gives UINavigationController a simple property that masks some complex behavior. If you set hidesBarsOnSwipe to be true for any UINavigationController, then iOS automatically adds a tap gesture recognizer to your view to handle hiding (and showing) the navigation bar as needed. This means you can mimic Safari's navigation bar behavior in just one line of code.

Upvotes: 1

JAL
JAL

Reputation: 42449

Every WKWebView has a scrollView property which allows you to access the UIScrollView part of the the web view. You can use the UIScrollViewDelegate method, scrollViewDidScroll to get callbacks on when the web view scrolls.

First, set the scroll view delegate:

let webView = WKWebView(...)
webView.scrollView.delegate = self

Then, implement the delegate method scrollViewDidScroll and add the logic to hide and show the navigation bar:

extension YourClass: UIScrollViewDelegate {

    func scrollViewDidScroll(scrollView: UIScrollView) {
        // you can use the position of the scrollView to show and hide your nav bar here
    }
}

Upvotes: 5

Related Questions