Mudith Chathuranga Silva
Mudith Chathuranga Silva

Reputation: 7434

want to navigate between tab bars by using swipe gesters in swift

I want to use a swipe gesture to navigate between tab bar controllers while keep the default tab bars. I used this code but shows an error.

import UIKit

 class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


        var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))


        leftSwipe.direction = .Left


        view.addGestureRecognizer(leftSwipe)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

        func handleSwipes(sender:UISwipeGestureRecognizer) {
            if (sender.direction == .Left) {

                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! UIViewController
                self.presentViewController(vc, animated: false, completion: nil)

            }

            if (sender.direction == .Right) {

            }
        }


    }


}

Upvotes: 7

Views: 8476

Answers (5)

mazenqp
mazenqp

Reputation: 355

copy ad paste this

class SwipeGesture: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let left = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft))
        left.direction = .left
        self.view.addGestureRecognizer(left)

        let right = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight))
        right.direction = .right
        self.view.addGestureRecognizer(right)

    }

    @objc func swipeLeft() {
        let total = self.tabBarController!.viewControllers!.count - 1
        tabBarController!.selectedIndex = min(total, tabBarController!.selectedIndex + 1)

    }

    @objc func swipeRight() {
        tabBarController!.selectedIndex = max(0, tabBarController!.selectedIndex - 1)
    }


}

Upvotes: 1

The best way for UITabBarViewController (Swift 3.0) will be next:

func handleSwipes(sender:UISwipeGestureRecognizer) {

    if sender.direction == UISwipeGestureRecognizerDirection.left {

        self.selectedIndex += 1

    } else if sender.direction == UISwipeGestureRecognizerDirection.right {

        self.selectedIndex -= 1
    }

}

Upvotes: 1

user2946704
user2946704

Reputation: 314

I could get your code working by adding the ID to the SecondViewController class as shown in the image below :

enter image description here

The only problem is, after getting this to work, you won't be able to see the Tab bar controller, since you have opened the SecondViewController which doesn't have the UI Tab bar controller.

Please let me know if my solution works and did you find any other way of swiping to second view and having the UI tab bar in place as well.

[EDIT] Actually I dug down a bit more and found out it was easier than I expected. The fix to our problem of tabBar controller not showing up is as follows :

   func handleSwipes(sender:UISwipeGestureRecognizer) {
        let selectedIndex: Int = self.tabBarController!.selectedIndex
        self.tabBarController!.selectedIndex = selectedIndex + 1
}

Upvotes: 1

Sandeep
Sandeep

Reputation: 21144

You should probably use UITabBarControllerDelegate methods to accomplish this. There are new delegate methods on UITabBarControllerDelegate that lets you return UIViewControllerAnimatedTransitioning and UIViewControllerInteractiveTransitioning.

These are the delegate methods that you would use,

- (id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController
                      interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController;

- (id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
            animationControllerForTransitionFromViewController:(UIViewController *)fromVC
                                              toViewController:(UIViewController *)toVC;

The Swift code for the delegate methods look like this,

func tabBarController(tabBarController: UITabBarController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?

func tabBarController(tabBarController: UITabBarController, animationControllerForTransitionFromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?

Upvotes: 0

ezcoding
ezcoding

Reputation: 2994

Your function handleSwipes must be a class level function, not an inner function of another function:

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

    // remove the func from here
}

func handleSwipes(sender:UISwipeGestureRecognizer) {
    if (sender.direction == .Left) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! UIViewController
        self.presentViewController(vc, animated: false, completion: nil)
    }

    if (sender.direction == .Right) {

    }
}

That is the obvious error I can see. You should always post your error message with your questions to improve the quality of answers.

Upvotes: 2

Related Questions