0
votes

I am new to swift. I am implementing tabbar controller in my Project and facing some design difficulty. My goal is when user click a tabbar item it should not navigate to another view controller. It should stayed in the current view and add a pop up view to the current view controller.I have tried but always it navigate to next view controller.

1
You can't achieve this through a UITabBarController. - PGDev
is there any other way to achieve this? - Saravanan

1 Answers

2
votes

Create a UITabBarController subclass and use that class for your tab bar controller. Confirm to UITabBarControllerDelegate in the tab bar controller and return false in tabBarController shouldSelect method when you don't want to navigate to a view controller. Here you can show the popup view.

class TabbarController: UITabBarController, UITabBarControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
    }
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if let navigationController = viewController as? UINavigationController,
            navigationController.viewControllers.contains(where: { $0 is MoreViewController }) {
            //show pop up view
            return false
        } else  {
            return true
        }
    }
}

Or you can add UITabBarControllerDelegate in one of its embedded view controller like this

class ViewController: UIViewController, UITabBarControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBarController?.delegate = self
    }
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if let navigationController = viewController as? UINavigationController,
            navigationController.viewControllers.contains(where: { $0 is MoreViewController }) {
            //show pop up view
            return false
        } else  {
            return true
        }
    }
}