Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebview allowsBackForwardNavigationGestures and UINavigationController interactivePopGestureRecognizer both enabled

My View Controller embedded inside a navigation controller holds a WKWebview on which I want to enable the following logic:

If a back item exists in the webview, making a left screen edge gesture should go back one page in the webview (normal behaviour of webview when allowsBackForwardNavigationGestures is set to yes). When there isnt any back item, it should pop one page in the navigation controller (interactivePopGestureRecognizer). I enabled both and I get random results, sometimes I go back one page in the webview and sometimes I go back to my home page in the navigation stack. I tried the following logic:

-(void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation {

  if (webView.backForwardList.backItem) {
    MYappDelegate.mainNavigationController.interactivePopGestureRecognizer.enabled = NO;
    self.webView.allowsBackForwardNavigationGestures = YES;
  } else {
    MYappDelegate.mainNavigationController.interactivePopGestureRecognizer.enabled = YES;
    self.webView.allowsBackForwardNavigationGestures = NO;
  }
}

but it crashes when I swipe and I get "WKCompositingView unrecognized selector sent to instance."

Any ideas where I'm going wrong? Im guessing there is a conflict regarding which gesture takes priority but I cant figure out where to add this logic so both the webview and the navigation controller containing its view controller live peacefully.

like image 670
Alok Sahay Avatar asked Jul 21 '16 09:07

Alok Sahay


1 Answers

_webView.allowsBackForwardNavigationGestures = YES;

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    self.navigationController.interactivePopGestureRecognizer.enabled = _webView.canGoBack ? NO : YES;
}
like image 131
罗大满 Avatar answered Oct 12 '22 18:10

罗大满