Hide UIToolbar UIBarButtonItems

谁说我不能喝 提交于 2019-11-26 22:23:32

问题


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!


回答1:


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.




回答2:


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




回答3:


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



回答4:


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




回答5:


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.




回答6:


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


来源:https://stackoverflow.com/questions/2971483/hide-uitoolbar-uibarbuttonitems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!