How to set first TabBar selected programmatically on iPhone
-
05-10-2019 - |
Question
I have UITabBar
in view which have 5 tabs. I am using didSelectItem
delegate to open different view i.e. I am NOT using TabBarController
.
My problem is on view load I need first tab get selected by default. Is there any property in TabBar which we can set to make it selected?
Thanks.
Solution
This code will work [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
OTHER TIPS
In swift if tabbar is used not tabbarcontroller set default select
var tabbar:CustomTabBar?//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
set the tabbar.selectedItem=0; in the viewWillAppear so when ever the view appears it will select the first tab by default.
[self.tabBar setSelectedItem:self.tabBar.items[0]];
or
self.tabBar.selectedItem = self.tabBar.items[0];
The selectedItem property requires a TabBarItem and not an index. So provide the tabbaritem in index 0 for the first tab.
This is wrong then: tabbar.selectedItem=0;
You may select other tabs as well. Happy coding