iosswipe-gestureuitabcontroller

TabController disappears when using swipe gesture


I have an app with a 3 tabs. I want to swipe right or left to go to another tab.

My code:

//Swipe Between Tabs
    let rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
    let leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
    rightSwipe.direction = .Right
    leftSwipe.direction = .Left
    view.addGestureRecognizer(rightSwipe)
    view.addGestureRecognizer(leftSwipe)
    //end Swipe

and the function to carry it out is

func handleSwipes(sender:UISwipeGestureRecognizer) {
    if (sender.direction == .Left) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("PantryList")
        let navigationController = UINavigationController(rootViewController: vc)

        self.presentViewController(navigationController, animated: true, completion: nil)
    }
    if (sender.direction == .Right) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("ToDoList")
        let navigationController = UINavigationController(rootViewController: vc)

        self.presentViewController(navigationController, animated: true, completion: nil)
    }
}

My problem is that tabBarController at the bottom disappears when swipe is used. From what I have found it has to do with the "presentViewController" method. Is this what is causing it and is there a way to do it without losing the tabBarController? I really don't want to use prepareForSegueWithIdentifier if I don't have to. That seems like more work than needs to be done unless that's how it has to be done.


Solution

  • if (sender.direction == .Right) {
        self.navigationController.tabBarController.selectedIndex = 1
    }