Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove UITabBarItem

How can I remove a UITabBarItem from a UITabBar?

I haven't tried anything, because I haven't found anything from Google searches or the documentation for UITabBar, UITabBarController, or UITabBarItem.

Thanks in advance! :)

like image 650
esqew Avatar asked Oct 27 '10 19:10

esqew


2 Answers

UITabBar has an NSArray collection of items. Since the items property is an NSArray and not an NSMutableArray, you'd have to construct a new NSArray from the existing one devoid of the object you want to remove, then set the items property to the new array.

/* 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];
like image 80
Mike Caron Avatar answered Sep 19 '22 06:09

Mike Caron


Mike Caron's advice will throw an exception if you intend to modify a tabBar that belongs to a controller.

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 should occur through the tab bar controller interface. You may still directly modify a tab bar object that is not associated with a tab bar controller.

In this case self.tabBarItem=nil will remove it.

like image 21
Özgür Avatar answered Sep 18 '22 06:09

Özgür