Search code examples
iosswiftuitabbar

How can I switch root UITabbar from anywhere in app?


Currently, I have a main uitabbar ( root view ) and have 5 tabs in it. If I want to trigger uitabbar to switch tab from anywhere in app. How can I do that ?

Thank you.


Solution

  • You'll need to get a reference to your tab bar controller and do something like this (providing your inside a UIViewController). If you have a custom UITabBarController class replace as? UITabBarController with as? YourClass.

    if let tabBarController = self.tabBarController as? UITabBarController {
        tabBarController.selectedIndex = 1
    }