Remove UITabBarItem - iphone

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! :)

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];

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.

NOTE: this appears to not work in iOS 11. It was still good in iOS 10.
This is a moderately horrible answer, in my opinion, in part because it's subverting the human interface guidelines, but all the same it seems to work cleanly:
UITabBar *oldbar = self.tabBarController.tabBar;
UITabBar *newbar = [[UITabBar alloc] initWithFrame:CGRectMake(0,0,oldbar.frame.size.width,oldbar.frame.size.height)];
NSMutableArray *olditems = [[oldbar items] mutableCopy];
[olditems removeObjectAtIndex:0];
NSArray *newitems = [[NSArray alloc] initWithArray:olditems];
[newbar setItems:newitems animated:false];
[oldbar addSubview:newbar];
That layers it cleanly on top of the old tabbar, and it maintains its functionality.

Related

Implementing my own navigation controller?

I have a tab bar app. Under one of the tabs i want a uisegmentedControl in the top navigation view, that controls what view is currently displayed. This is dead easy if i just exchange the view, but i want to do it in a more organized and generic way, by using one uiviewcontroller for each view and exchanging them in the most optimzed way.
i guess step one would be to know exactly what a tabbar controller sends to a navigation controller/view controller when a tab is changed, and work it out from there.
Can any one point me in the right direction?
Some time ago I stumbled upon SegmentsController which I found in this blog entry from red artisan.
I used it in conjunction with a UITabBarController, but without knowing I did it wrong. Not wrong as in "it crashs" or "it doesn't do what i want" but wrong in the sense that I have to forward each UIViewController call (like viewDidAppear, receivedMemoryWarning etc) to the child viewControllers. The app with the wrong code is still in the app store and I never received a complain about it.
But I played around a while and figured out how to use it right. It's a bit of a hassle but imho it's absolutely worth it.
I'll show you the correct version that I have right now, I'm creating the UITabBarController in Interface Builder so I have to change the tab in code. Which introduces another piece of mess, and maybe there is room for improvements. But right now I'm satisfied with this solution.
NSMutableArray *items = [self.tabBarController.viewControllers mutableCopy]; // tabs from tabbar configured in IB
// The two child vc that will appear in the segment control
SomeViewController_iPhone *tvcs = [[[SomeViewController_iPhone alloc] initWithNibName:#"SomeView_iPhone" bundle:nil] autorelease];
SomeOtherViewController_iPhone *tvct = [[[SomeOtherViewController_iPhone alloc] initWithNibName:#"SomeOtherView_iPhone" bundle:nil] autorelease];
NSArray *viewControllers1 = [NSArray arrayWithObjects:tvcs, tvct, nil];
// the nav controller acts as a wrapper around the child viewcontrollers
UINavigationController *navController1 = [[[UINavigationController alloc] init] autorelease];
navController1.tabBarItem.title = NSLocalizedString(#"FirstTab", nil);
navController1.tabBarItem.image = [UIImage imageNamed:#"tabImage1.png"];
navController1.navigationBar.tintColor = [UIColor navBarTintColor];
firstTabSegmentsController = [[SegmentsController alloc] initWithNavigationController:navController1 viewControllers:viewControllers1];
// uses a NSArray category that basically creates a NSArray that has the title properties of the vc in viewControllers1
firstTabSegmentedController = [[UISegmentedControl alloc] initWithItems:[viewControllers1 arrayByPerformingSelector:#selector(title)]];
firstTabSegmentedController.frame = CGRectMake(0, 0, 222, 30);
firstTabSegmentedController.segmentedControlStyle = UISegmentedControlStyleBar;
firstTabSegmentedController.selectedSegmentIndex = 0;
[firstTabSegmentsController indexDidChangeForSegmentedControl:firstTabSegmentedController];
[firstTabSegmentedController addTarget:firstTabSegmentsController action:#selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
// replace first tab from interface builder with this
[items replaceObjectAtIndex:0 withObject:navController1];
as you see it needs a bit of setup, but in my opinion this solution is better than anything else I've tried throughout the time. I hope I de-NDAed the code correctly.
Edit: Uploaded a sample project: BeautifulColors.zip
Just exchanging the views and keeping up with the current view's viewController is the best way to implement a UISegmentedControl in this regard.
Note: by exchanging the views i mean adding a subview to the current view and removing the old one.
You might be interested in the method below, which is implemented by the UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;

Reload / Refresh tab bar items in a ViewController ?

I am trying to change images of my tabbar in a ViewController, but to display the new images, I must click on each tab bar item.
for (CustomTabBarItem *myItem in self.tabBarController.tabBar.items){
myItem.enabled = YES;
myItem.badgeValue = #"1";
UIImage *myImage = [[UIImage alloc] initWithContentsOfFile:[[DesignManager sharedManager] getPathOfFile:#"test.png"]];
*myItem.imageSelect= *myImage; // change images of each item. don't appear if I dont click on the item
}
Anyone know How can I can display directly these images?
Thanks
You need to replace the old tab bar item with a new one. You can't update the image dynamically otherwise.
The easiest way to do this is to set the tabBarItem property of the view-controller represented by a given tab. If you wanted to do this from within that view controller, just write:
self.tabBarItem = [[UITabBarItem alloc] initWithTitle: #"title" image: myImage: tag: nil];
Or, you could do this from somewhere else, say your app delegate:
UIViewController* vc = [tabBarController.viewControllers objectAtIndex: 3];
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle: #"title" image: myImage: tag: nil];
I know this is an old question. I ran into the same problem when I need to update the badge value from another active tab. Creating another UITabBarItem will solve your current problem but causes potential memory leak when this code is called many times. Plus, when other view controllers access the tab, they do not have reference to newly created UITabBarItem. My trick is
vc.tabBarItem = vc.tabBarItem;
It works for me.

How to add a badge to a UITabBar that is customizable?

I am adding a badge to my UITabBarController's UITabBar as such:
UITabBarItem *tbi = (UITabBarItem *)[stTabBarController.tabBar.items objectAtIndex:1];
tbi.badgeValue = #"2";
However, my UITabBarController is customizeable, so the index may change. How can I make sure the badge gets applied to the correct UITabBarItem?
One suggestion you might consider is setting a tag on each tab bar item. You can do this in Interface Builder or when you create the item by code. You can then loop through the view controllers in the tab bar controller looking for the one with the tab bar item you are interested in. For example:
// #define MyTabBarItemTag 999
for (UIViewController *viewController in stTabBarController.viewControllers) {
if (viewController.tabBarItem.tag == MyTabBarItemTag) {
viewController.tabBarItem.badgeValue = #"2";
}
}
UITabBarItem *tbi = (UITabBarItem *)self.tabController.selectedViewController.tabBarItem;
tbi.badgeValue = #"New";
Also works.
Swift version:
self.tabBarController?.selectedViewController?.tabBarItem.badgeValue="12";
I'd use an NSMutableDictionary property on the class that owns the tab bar controller, associating tab names with positions, and a method to retrieve by name:
-(UITabBarItem*)getTabByName:(NSString*)tabName {
return [stTabBarController.tabBar.items objectAtIndex:[[tabDict valueForKey:tabName] intValue]];
}
Initialize the dictionary in your setup code for each tab, since you know the tab index at that time:
[tabDict setValue:[stTabBarController.tabBar.items objectAtIndex:1] forKey:#"myTabName"];
Keep a reference to the tab bar item that you want to modify.
EDIT as a response to your code request:
I believe that there is a single place in your app where you update the badges on the tab bar items. Just add an array of tab bar items (or separate tab bar items) as a member(s) of that class (+ properties if needed) and update the items directly without fetching from the current tab bar items list ((UITabBarItem *)[stTabBarController.tabBar.items objectAtIndex:1];).
For instance, if you decide to keep references to the tab bar items directly (without an array) then the code might look like that:
// Put the next code right after initiating the tab bar and/or after adding new tab bar items to it...
self.newsTabBarItem = (UITabBarItem *)[stTabBarController.tabBar.items objectAtIndex:1];
self.friendsTabBarItem = (UITabBarItem *)[stTabBarController.tabBar.items objectAtIndex:2];
// etc.

Change title of UItabbaritem in other view?

How do I change the UITabbaritem titles when starting the App?
I got a UITabBar with 4 tabs. I want the user to be able to change between two different languages. If the user chooses a different language I need to set a different title to the UITabbaritems. I know I can use self.title = #"title"; but that only changes the current tabbaritem title. How can I change all the titles at once, on load, and when choosing a different language?
I got the same problem and solved it with.
[[self.tabBarController.viewControllers objectAtIndex:index] setTitle:#"Your title"];
Where index is your UITabBarItem index.
You need to store all your UITabBarItem into an array, when the user tap the button, you need to loop through that array and set the title.
for (UITabBarItem *item in items) {
item.title = #"WHATEVER HERE";
}

Hide UIToolbar UIBarButtonItems

I have a UIToolbar that I set up using IB with three buttons, left, middle and right. In some situations I would like to not display the middle button. Does anybody know of a way to hide a specific button on inside a UIToolBar? There is no hide property, all I can find is setEnable but this still leaves the button causing users to wonder what its purpose is. I would like to only display it in situations that it actually has a use.
Thanks in advance!
Reset the items:
-(void)setItems:(NSArray *)items animated:(BOOL)animated
You can get the current items using the items property, then just remove the one you don't want to show and pass in the new NSArray.
As you can see, you can also animate it to make it clear to the user.
Rather than guessing at the index, I added an IBOutlet for the UIBarButtonItem and then removed it by name:
NSMutableArray *toolBarButtons = [self._toolbar.items mutableCopy];
[toolBarButtons removeObject:self._selectButton]; // right button
[self._toolbar setItems:toolBarButtons];
And of course it helps to connect the outlets in the designer :)
This is how i did it.. too much headache but its the best i could come up with :
NSArray *toolBarArray = toolBar.items;
NSMutableArray *newToolBarArray = [NSMutableArray arrayWithArray:toolBarArray];
[newToolBarArray removeObjectAtIndex:2];
[newToolBarArray removeObjectAtIndex:1];
//remove whatever buttons you want to.
NSArray *finalTabBarArray =[[NSArray alloc] initWithObjects:newToolBarArray, nil];
[toolBar setItems:[finalTabBarArray objectAtIndex:0] animated:NO];
I know it is quite old thread for but those who look this page for solution, here you go :
With iOS7, you can use this approach to show/hide your toolbar button :
if(// your code Condition)
{ self.toolbarBtn1.enabled = YES;
self.toolbarBtn1.tintColor = nil; }
else
{ self.toolbarBtn1.enabled = NO;
self.toolbarBtn1.tintColor = [UIColor clearColor]; }
This does not work here because the array you are sending with setItem is not what the function expects.
I had to replace the line:
NSArray *finalTabBarArray = [[NSArray alloc] initWithObjects:newToolBarArray, nil];
with this one:
NSArray *finalTabBarArray = [newToolBarArray copy];
Then it works perfectly.
Mohit's answer is one that I have used, but you dont need to specifically make it a NSArray that the toolbar sets. You can just set the array of items as a NSMutableArray. No real advantage that I am aware off but its a few lines less code. And that way you can take the array and move about UIButton objects as you would any other array with objects and then just reset the toolbar with that mutable array.
[newToolBarArray removeObjectAtIndex:2];
[newToolBarArray removeObjectAtIndex:1];
[toolBar setItems:newToolBarArray];