Prevent automatic popToRootViewController on double-tap of UITabBarController

前端 未结 5 1955
别跟我提以往
别跟我提以往 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 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!!!

提交回复
热议问题