Reputation: 1719
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
Upvotes: 27
Views: 41142
Reputation: 341
In Xamarin.ios, we can use like this mainTabBarController.selectedIndex=3;
Upvotes: -1
Reputation: 567
if you inside UITabBarController you can useself.selectedIndex = // set target index
Upvotes: 5
Reputation: 1004
Swift 3:
tabBarController.selectedIndex = 0 // (or any other existing index)
Upvotes: 20
Reputation: 5
Before select active tab bar item on viewDidLoad event
[self.tabBar setSelectedItem: [self.tabBar.items objectAtIndex:0]];
Upvotes: 0
Reputation: 1366
In swift if tabbar is used not tabbarcontroller set default select
var tabbar:UITabBar?//if declare like this
tabbar!.selectedItem = self.tabbar!.items![0] as? UITabBarItem
or
let tabbar = UITabBar()//if declare and initilize like this
tabbar.selectedItem = self.tabbar.items![0] as? UITabBarItem
Upvotes: 6
Reputation: 908
[tabBar setSelectedItem: [tabBar.items objectAtIndex:0]];
Which in swift, I think would be:
tabBar.selectedItem = tabBar.items![0] as UITabBarItem
Upvotes: 49