Search code examples
iphonecocoa-touchuitabbarcontrolleruitabbar

Disable/hide uitabbaritem on an uitabbar


I found this code here that i think will do the job.

`/* suppose we have a UITabBar *myBar, and an int index idx */  
NSMutableArray modifyMe = [[myBar items] mutableCopy];  
[modifyMe removeObjectAtIndex:idx];  
NSArray *newItems = [[NSArray alloc] initWithArray:modifyMe];  
[myBar setItems:newItems animated:true];`

The question is, where should i write this code in order to work?
I tried putting it in viewDidLoad of the UITabBarController but it didn't work.


Solution

  • If you're modifying the tabbar items from the UITabbarController, you can't use setItems:animated:. From the docs:

    In iOS 3.0 and later, you should not attempt to use the methods and properties of this class to modify the tab bar when it is associated with a tab bar controller object. Modifying the tab bar in this way results in the throwing of an exception. Instead, any modifications to the tab bar or its items must occur through the tab bar controller interface.

    Instead, swap out the viewControllers property of your UITabbarController, removing the UIViewController that corresponds to the tabbar item you want removed. For example, if you want to remove the 2nd tabbar item:

    NSMutableArray *newViewControllers = [NSMutableArray arrayWithArray:self.viewControllers];
    [newViewControllers removeObjectAtIndex:1];
    [self setViewControllers:newViewControllers];