开发者

Hide UIToolbar UIBarButtonItems

开发者 https://www.devze.com 2023-01-02 11:12 出处:网络
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 di开发者_StackOverflow中文版splay the middle button.Does anybody know of a

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 di开发者_StackOverflow中文版splay 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];
0

精彩评论

暂无评论...
验证码 换一张
取 消