Hide UIToolbar UIBarButtonItems
Asked Answered
M

6

14

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!

Masker answered 4/6, 2010 at 4:19 Comment(0)
N
28

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.

Nigrescent answered 4/6, 2010 at 4:26 Comment(2)
Ahh yes, this works. For some reason I originally thought it wouldn't but it does. Thanks!Masker
@JayQ. this doesn't hide a button: it removes it. To hide the button, you'd have to access its view via valueForKey:@"_view" which might or might not be ok. Personally, I think it's fine.Foreshorten
A
12

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

Andizhan answered 7/8, 2013 at 23:23 Comment(1)
nicely concise Nico, even with the unorthodox variable naming for your properties. Typically the "_" is associated with the auto-synthesized variables and not the property themselves. ;)Sumatra
M
6

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];
Mulligan answered 12/7, 2012 at 7:23 Comment(0)
L
2

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

Lubberly answered 29/4, 2016 at 16:40 Comment(1)
I cannot get it working. My toolbar is black and I can still see the button (tested on iOS 10).Olivier
C
1

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.

Chavarria answered 12/7, 2012 at 8:55 Comment(0)
L
1

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];
Llewellyn answered 18/9, 2012 at 0:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.