Prevent automatic popToRootViewController on double-tap of UITabBarController

前端 未结 5 1942
别跟我提以往
别跟我提以往 2020-11-30 07:18

The default behavior of a UITabBarController is to pop the contained UINavigationController to the root view controller when a particular tab is tapped a second time. I have

相关标签:
5条回答
  • 2020-11-30 07:44

    This behavior is a little strange, but a handy shortcut in case of deep hierarchy!

    You can implement following UITabBarControllerDelegate methods to disable this system wide shortcut:

    #pragma mark -
    #pragma mark UITabBarControllerDelegate
    
    - (BOOL)tabBarController:(UITabBarController *)tbc shouldSelectViewController:(UIViewController *)vc {
        UIViewController *tbSelectedController = tbc.selectedViewController;
    
        if ([tbSelectedController isEqual:vc]) {
            return NO;
        }
    
        return YES;
    }
    
    0 讨论(0)
  • 2020-11-30 07:54

    this is what I did:

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 
    {
    
        if ([[tabBarController viewControllers] objectAtIndex:[tabBarController selectedIndex]] == viewController)
    
                return NO;
    
        return YES;
    
    }
    

    regards

    0 讨论(0)
  • 2020-11-30 08:05

    Use the tabBarController:shouldSelectViewController: method of the UITabBarControllerDelegate protocol.

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
        return viewController != tabBarController.selectedViewController;
    }
    

    Don't forget to set the delegate of the tab bar controller to the object that actually implements this delegate method.

    0 讨论(0)
  • 2020-11-30 08:06

    Update Swift 4.1

    Stop Double Tap for all tabs.

    extension TabBarController: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        //for blocking double tap on all tabs.
        return viewController != tabBarController.selectedViewController
    }}
    

    Stop Double Tap on only one specific tab. Here it's for 3rd Tab.

    extension TabBarController: UITabBarControllerDelegate {
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        //for blocking double tap on 3rd tab only
        let indexOfNewVC = tabBarController.viewControllers?.index(of: viewController)
        return ((indexOfNewVC != 2) ||
            (indexOfNewVC != tabBarController.selectedIndex))       
    }}
    

    Hope it helps...

    Thanks!!!

    0 讨论(0)
  • 2020-11-30 08:10

    Here is the Swift 3 version:

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        return viewController != tabBarController.selectedViewController
    }
    
    0 讨论(0)
提交回复
热议问题