Swipe back / forward in UIWebView?

I have a webview in my application.

Since this is a tabbed application, I cannot add buttons to return / forward to the website.

I want to go back / forward by checking. The correct swipe on the left side / edge again ... like in Safari for iOS.

How can i do this? I think I should use the "Recognizer Gesture Screen Edge Pan", right?

+5
source share
3 answers

Why not just use a hard gesture recognizer?

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)]; // Setting the swipe direction. [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; // Adding the swipe gesture on WebView [webView addGestureRecognizer:swipeLeft]; [webView addGestureRecognizer:swipeRight]; - (void)handleSwipe:(UISwipeGestureRecognizer *)swipe { if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { NSLog(@"Left Swipe"); } if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { NSLog(@"Right Swipe"); } } 
+3
source

Accepted answer in Swift 3:

 override func viewDidLoad() { super.viewDidLoad() let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:))) let swipeRightRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:))) swipeLeftRecognizer.direction = .left swipeRightRecognizer.direction = .right webView.addGestureRecognizer(swipeLeftRecognizer) webView.addGestureRecognizer(swipeRightRecognizer) } @objc private func handleSwipe(recognizer: UISwipeGestureRecognizer) { if (recognizer.direction == .left) { if webView.canGoForward { webView.goForward() } } if (recognizer.direction == .right) { if webView.canGoBack { webView.goBack() } } } 
+11
source

Answer in Swift 3 and Swift 4

If anyone still has problems. This worked for me:

Find "didFinish" and add / replace the following code.

 func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { self.didFinish() webView.allowsBackForwardNavigationGestures = true } 

The main code you need is just one line. It appears after self.didFinish() , but is still in brackets {} .

 webView.allowsBackForwardNavigationGestures = true 
+1
source

All Articles