0

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.

Will Zimmer
  • 111
  • 10

3 Answers3

1

Of course it's because you are presenting view controller on top of current view controller. To switch between UITabbarController viewControllers, you can use setSelectedIndex: method, in your case, your first vc will have 0 index, second and third 1 and 2 respectively. Just switch the selected index on swipe, and you are done! Good luck!

Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29
1
if (sender.direction == .Right) {
    self.navigationController.tabBarController.selectedIndex = 1
}
MobileMon
  • 8,341
  • 5
  • 56
  • 75
  • awesome, works how I wanted. Except one thing...I did enjoy the transition of the screen sliding up from the bottom when it transitioned. I have no idea how to do the transitions and lucked out on the last one haha. How can I get this back with the 'selectedIndex:' method? – Will Zimmer Jan 28 '16 at 13:50
0

I too think your problem is with the presentViewController:. Assuming the swipe handling code is in the UITabBarController, what you are doing here is pushing a VC on top of the whole tabcontroller.

I'll try to move the selected view controller's view along the next VC view to be displayed (in the direction given by the swipe move), then change the value of the UITabBarController selectedViewController to your new VC.