我有一个使用IB设置了三个按钮(左、中、右)的UIToolbar。在某些情况下,我希望不显示中间的按钮。是否有方法可以隐藏UIToolBar中特定的按钮?没有隐藏属性,我只能找到setEnable,但这仍然会留下按钮,让用户想知道它的作用是什么。我希望只在实际有用的情况下显示它。
提前感谢!
提前感谢!
我添加了一个UIBarButtonItem的IBOutlet,而不是猜测索引,然后通过名称将其删除:
NSMutableArray *toolBarButtons = [self._toolbar.items mutableCopy];
[toolBarButtons removeObject:self._selectButton]; // right button
[self._toolbar setItems:toolBarButtons];
当然,将插座连接到设计师中也有帮助 :)
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];
我知道这个帖子已经有点老了,但是对于那些在寻找解决方案的人,这里提供一个方法:
在iOS7中,您可以使用以下方法来显示/隐藏工具栏按钮:
if(// your code Condition)
{ self.toolbarBtn1.enabled = YES;
self.toolbarBtn1.tintColor = nil; }
else
{ self.toolbarBtn1.enabled = NO;
self.toolbarBtn1.tintColor = [UIColor clearColor]; }
这里不起作用,因为您使用setItem发送的数组与函数所期望的不同。
我不得不替换这行:
NSArray *finalTabBarArray = [[NSArray alloc] initWithObjects:newToolBarArray, nil];
用这个:
NSArray *finalTabBarArray = [newToolBarArray copy];
那么它就完美地运作了。
[newToolBarArray removeObjectAtIndex:2];
[newToolBarArray removeObjectAtIndex:1];
[toolBar setItems:newToolBarArray];
valueForKey:@"_view"
访问其视图,这可能是可以的,也可能不可以。就我个人而言,我认为这样做没问题。 - Dan Rosenstark