Programmatically change the highlighted state of a UITabBarItem - Objective C
-
29-09-2019 - |
Question
I would like to know how to make the UITabBarItem be highlighted when I choose to do so in the program. Is this possible?
THanks
Solution
You can change which tab is selected -- which also changes the highlight -- easily:
tabcontroller.selectedIndex = youNewIndexHere;
If your tabcontroller is in the app delegate (which is the case if you used the tab bar application template when you created your project) it would look something like this:
ProjectNameAppDelegate *appDelegate = (ProjectNameAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate tabcontroller].selectedIndex = youNewIndexHere;
OTHER TIPS
tabbar works if you know the index and write it like this:
NSMutableArray *item [[NSMutableArray alloc] init];
[items addObject: [[[UITabBarItem alloc] initAsULike] autorelease]; <--- init as you like here
...
[tabBar setItems:items animated:FALSE];
tabBar.selectedItem = [items objectAtIndex: your_index]; <--- write your index here
[items release];
I've got a UITabBar, created in IB but without a TabBarController. In my awakeFromNib I just initialise it to the first item like this:
[self.tabBar setSelectedItem:[[self.tabBar items] objectAtIndex:0]];
Don't forget, the UITabBarDelegate didSelectItem does not get called in this case.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow