Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a tab bar item programmatically (not using UITabBarController)

I have a view derived from UIViewControler (not UITabBarController). In this view I added a tab bar with several tab bar items. I used the UITabBarDelegate to allow the view to do something when users tap on each tab bar item.

class MyViewController: UIViewController, UITabBarDelegate {      func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {         // do something     } } 

My question is how we can programmatically select the first tab bar item when the view is first loaded? Note that I would want the first tab item to be in "active" state also.

Again, I'm not using UITabBarController

Thanks

like image 643
tala9999 Avatar asked Mar 26 '15 18:03

tala9999


People also ask

How do I add a tab bar to a storyboard?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

What is tab bar in Swift?

The tab bar interface displays tabs at the bottom of the window for selecting between the different modes and for displaying the views for that mode. This class is generally used as-is, but may also be subclassed. Each tab of a tab bar controller interface is associated with a custom view controller.


1 Answers

[tabBar setSelectedItem: [tabBar.items objectAtIndex:0]]; 

Which in swift, I think would be:

tabBar.selectedItem = tabBar.items![0] as UITabBarItem 
like image 134
Foster Bass Avatar answered Sep 21 '22 23:09

Foster Bass