Search code examples
swiftuinavigationcontrolleruitabbarcontroller

Dismiss current navigation controller when clicked tab bar


I have got a tab bar controller and in home view controller i have a navigation controller.

-Tab Bar Controller

-- HomeVC

--- VC1 navigation push -> VC2

In VC1 navigation bar is not hidden but inside VC2 is hidden. And im controlling it with viewwillappear and viewwilldisappear.

    override func viewWillAppear(_ animated: Bool) {
    navigationController?.navigationBar.barStyle = .blackTranslucent
}
override func viewWillDisappear(_ animated: Bool) {
    navigationController?.navigationBar.isHidden = false
}

But turning back to the VC1 without swipe, I mean clicking tab bar homeVC icon hides navigation bar. I want to dismiss or pop current viewcontroller and turn back to VC1.


Solution

  • So you can do this by popToRootViewController of UINavigationController. you have to handle this in tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) method of UITabBarDelegate.

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        if let rootView = self.viewControllers!["Index of VC1 Controller"] as? UINavigationController {
            rootView.popToRootViewController(animated: false)
        }
    }