Jon Setting Jon Setting - 2 years ago 232
Swift Question

Change UITabbar selectedItem in Swift 2.0

I have spent the past 2 hours trying to figure this out. I basically want to write

tabBar.selectedItem = someItemOtherThanItemAtIndexZero

Any ideas?

Answer Source

Try the following

tabBar.selectedItem = tabBar.items![newIndex] as! UITabBarItem


Assuming you have access to the UITabBarController that owns the UITabBar, you can do the following

self.selectedViewController = self.viewControllers![newIndex] as! UIViewController

The above line of code should be put somewhere inside of the UITabBarController subclass.

But if you have access to the tab bar controller from "outside," do the following

tabBarController.selectedViewController = tabBarController.viewControllers![newIndex] as! UIViewController
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download