我在IB中创建了一个带有几个按钮的工具栏。我想能够隐藏/显示一个按钮取决于数据的状态在主窗口。
UIBarButtonItem没有隐藏属性,到目前为止我发现的任何隐藏它们的例子都涉及将导航栏按钮设置为nil,我不认为我想在这里做,因为我可能需要再次显示按钮(更不用说,如果我连接我的按钮到IBOutlet,如果我设置为nil,我不确定我如何得到它)。
我在IB中创建了一个带有几个按钮的工具栏。我想能够隐藏/显示一个按钮取决于数据的状态在主窗口。
UIBarButtonItem没有隐藏属性,到目前为止我发现的任何隐藏它们的例子都涉及将导航栏按钮设置为nil,我不认为我想在这里做,因为我可能需要再次显示按钮(更不用说,如果我连接我的按钮到IBOutlet,如果我设置为nil,我不确定我如何得到它)。
当前回答
补充Eli Burke的回应,如果你的uibarbuttonitem有一个背景图像而不是一个标题,你可以使用代码:
-(void)toggleLogoutButton:(bool)show{
if (show) {
self.tabButton.style = UIBarButtonItemStyleBordered;
self.tabButton.enabled = true;
UIImage* imageMap = [UIImage imageNamed:@"btn_img.png"];
[((UIButton *)[self.tabButton customView]) setBackgroundImage:imageMap forState:UIControlStateNormal];
} else {
self.tabButton.style = UIBarButtonItemStylePlain;
self.tabButton.enabled = false;
[((UIButton *)[self.tabButton customView]) setBackgroundImage:nil forState:UIControlStateNormal];
}
}
其他回答
这是一个很长的答案列表,但为了防止有人想要一个简单的复制和粘贴的快速解决方案,这里是
func hideToolbarItem(button: UIBarButtonItem, withToolbar toolbar: UIToolbar) {
var toolbarButtons: [UIBarButtonItem] = toolbar.items!
toolbarButtons.removeAtIndex(toolbarButtons.indexOf(button)!)
toolbar.setItems(toolbarButtons, animated: true)
}
func showToolbarItem(button: UIBarButtonItem, inToolbar toolbar: UIToolbar, atIndex index: Int) {
var toolbarButtons: [UIBarButtonItem] = toolbar.items!
if !toolbarButtons.contains(button) {
toolbarButtons.insert(button, atIndex: index)
toolbar.setItems(toolbarButtons, animated:true);
}
}
我认为我将根据lnafziger接受的答案分享一些帮助方法,因为我在每个工具栏中都有多个按钮:
-(void) hideToolbarItem:(UIBarButtonItem*) button inToolbar:(UIToolbar*) toolbar{
NSMutableArray *toolbarButtons = [toolbar.items mutableCopy];
[toolbarButtons removeObject:button];
[toolbar setItems:toolbarButtons animated:NO];
}
-(void) showToolbarItem:(UIBarButtonItem*) button inToolbar:(UIToolbar*) toolbar atIndex:(int) index{
NSMutableArray *toolbarButtons = [toolbar.items mutableCopy];
if (![toolbarButtons containsObject:button]){
[toolbarButtons insertObject:button atIndex:index];
[self setToolbarItems:toolbarButtons animated:YES];
}
}
我有一个问题,我有2 leftBarButtonItems。在Mac Catalyst上,firstButton指向一个不支持的操作:用AVFoundation录制视频。Mac Catalyst上只有第二个按钮有效:使用UIImagePickerController。
所以在Mac Catalyst上,我必须将第一个UIBarButtonItem指向secondButton,并始终隐藏第二个UIBarButtonItem。在iOS上,两个按钮都应该显示出来。这就是我的解决方案:
#if TARGET_OS_MACCATALYST
self.navigationItem.leftBarButtonItem = self.secondButton;
NSUInteger count = [self.navigationItem.leftBarButtonItems count];
for (NSUInteger i = 0; i < count; i++) {
UIBarButtonItem *thisButton = [self.navigationItem.leftBarButtonItems objectAtIndex:i];
if (i == 1) {
thisButton.enabled = NO;
thisButton.tintColor = [UIColor clearColor];
}
}
#else
self.navigationItem.leftBarButtonItem = self.firstButton;
#endif
我希望它能帮助到有类似问题的人。
归功于@lnafziger、@MindSpiker、@vishal等。都
我为单个右(或左)栏按钮设计的最简单的一行代码是:
self.navigationItem.rightBarButtonItem = <#StateExpression#>
? <#StrongPropertyButton#> : nil;
如:
@interface MyClass()
@property (strong, nonatomic) IBOutlet UIBarButtonItem *<#StrongPropertyButton#>;
@end
@implementation
- (void) updateState
{
self.navigationItem.rightBarButtonItem = <#StateExpression#>
? <#StrongPropertyButton#> : nil;
}
@end
我测试了这个,它为我工作(与强栏按钮项目有线通过IB)。
可以在不改变其宽度或将其从工具条中移除的情况下将按钮隐藏在适当的位置。如果将样式设置为普通,删除标题并禁用按钮,则该按钮将消失。要恢复它,只需反转您的更改。
-(void)toggleBarButton:(bool)show
{
if (show) {
btn.style = UIBarButtonItemStyleBordered;
btn.enabled = true;
btn.title = @"MyTitle";
} else {
btn.style = UIBarButtonItemStylePlain;
btn.enabled = false;
btn.title = nil;
}
}