Make a tab bar button open the right side menu

1.2k Views Asked by At

Say i have a tab bar with 5 ViewControllers, And a left and right menus.

My deck ViewController holds the tab bar as a center ViewController:

  self.tabBarController.viewControllers = @[UINAVIGATIONCONTROLLER(feedController),
                                              UINAVIGATIONCONTROLLER(myProfileController),
                                              UINAVIGATIONCONTROLLER(topTradersController),
                                              UINAVIGATIONCONTROLLER(stocksListController),
                                            //  UINAVIGATIONCONTROLLER(watchlistController),
                                            ];





    self.deckController =  [[STViewDeckController alloc] initWithCenterViewController:self.tabBarController
                                                                   leftViewController:settingsController
                                                                  rightViewController:watchlistController];

I would like to make on of the tab bar buttons open the right side menu, instead of acutely making the tabBar push that ViewController.

Say I would like the "watchlistController" tabBar button open the side menu, How could i do that?

Oh, and I'm using "IIViewDeckController" for deckController

Thanks

2

There are 2 best solutions below

1
On

You can prevent a tabBarController from showing the view/viewController associated with the tab by using theUITableControllerDelegate and the method - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

Add the following to your code:

self.tabBarController.delegate = self;

and then implement this:

#pragma mark - UITabBarControllerDelegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    BOOL isLeft = YES;
    if (isLeft) {
        [self.deckController openLeftView];
    }
    else {
        [self.deckController openRightView];
    }

    return NO;
}

This should do the trick for you.

0
On

Swift 5 - Open SideMenu from Tabbar Item click

This answer is a swift version of @mbogh's answer and credit goes to him for producing this trick.Please follow these steps to open side menu on tabbar item click.

//make your controller subclass of UITabBarControllerDelegate
extension HomeVC: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        self.openSideMenu()
        return false
    }
}

//triger tabbbar delegate from  your controller's ViewDidLoad()
self.tabBarController?.delegate = self