ryder
ryder

Reputation: 155

iOS Disable popping to root view on pressing tab bar button

I'm looking to disable the functionality of pressing my tab bar and returning to the root view in the navigation hierarchy whilst also keeping this functionality for a button in my navigation bar.

So the only way I want the user to go back to the root view is by pressing the button in the navigation bar and not by tapping the tab bar button.

I've had a look around and tried several solutions but none seem to work as they disable the functionality for both the nav bar button and the tab bar button but not just the tab bar button.

Thanks!

Upvotes: 9

Views: 3567

Answers (2)

Charlie S
Charlie S

Reputation: 4594

Came across this issue at the weekend. I kept finding my custom TabBarController was nil during appDelegate didfinishlaunching() method.

The way I got around it was to make my custom TabBarController a UITabBarControllerDelegate and then implemented the following delegate method withint he custom TabBarController class:

    // Stops View Controllers from being dismissed when a tab is selected on the UITabBarController
    public func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    return viewController != tabBarController.selectedViewController
    }

Upvotes: 3

croX
croX

Reputation: 1725

A possibility would be to create a subclass of UITabBarController and to implement the UITabBarControllerDelegate protocol. Then you could implement

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

and return NO, when the particular viewController is selected again. A way to do this is is to save the recently selected item and to compare it with the currently selected one.

Upvotes: 4

Related Questions