hemangshah / HHTabBarView

A lightweight customized tabbar view. 📌
MIT License
154 stars 20 forks source link

Go back in navigation hierarchy when tapping on the same tab again. #16

Closed hemangshah closed 5 years ago

hemangshah commented 5 years ago

Tab 1 ↠ Navigation Controller ↦ View Controller 1 → View Controller 2 → View Controller 3

So we're in View Controller 3 and by tapping on the same tab (Tab 1) again, I will now take back to View Controller 1 (the first view-controller in navigation controller hierarchy).

To achieve this, I've made a slight change in on tab tapped closure block:

        hhTabBarView.onTabTapped = { (tabIndex, isSameTab, controller) in
            if isSameTab {
                if let navcon = controller as? UINavigationController {
                    navcon.popToRootViewController(animated: true)
                } else if let vc = controller as? UIViewController {
                    vc.navigationController?.popToRootViewController(animated: true)
                }
            }
            print("Selected Tab Index:\(tabIndex)")
        }

With this, there'll be a possible highlight issue will occurs as part of this change in 2.3.0, to fix this:

I am suggesting this solution: Integrate this functions in View Controller 1 (and all other where you will have similar navigation flow).

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        // This will be helpful in locking the current tab if we're already in the first view-controller in navigation flow.
        HHTabBarView.shared.lockCurrentTab()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // This will be helpful to unlock the locked tab when we're going forward from the first view-controller in navigation flow.
        HHTabBarView.shared.unlockAllTabs()
    }

P.S. Thanks Nishan for coming up with this issue. 🙏🏼