我在IB中创建了一个带有几个按钮的工具栏。我想能够隐藏/显示一个按钮取决于数据的状态在主窗口。
UIBarButtonItem没有隐藏属性,到目前为止我发现的任何隐藏它们的例子都涉及将导航栏按钮设置为nil,我不认为我想在这里做,因为我可能需要再次显示按钮(更不用说,如果我连接我的按钮到IBOutlet,如果我设置为nil,我不确定我如何得到它)。
我在IB中创建了一个带有几个按钮的工具栏。我想能够隐藏/显示一个按钮取决于数据的状态在主窗口。
UIBarButtonItem没有隐藏属性,到目前为止我发现的任何隐藏它们的例子都涉及将导航栏按钮设置为nil,我不认为我想在这里做,因为我可能需要再次显示按钮(更不用说,如果我连接我的按钮到IBOutlet,如果我设置为nil,我不确定我如何得到它)。
当前回答
可以在不改变其宽度或将其从工具条中移除的情况下将按钮隐藏在适当的位置。如果将样式设置为普通,删除标题并禁用按钮,则该按钮将消失。要恢复它,只需反转您的更改。
-(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;
}
}
其他回答
补充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];
}
}
下面是我的解决方案,虽然我正在寻找它的导航栏。
navBar.topItem.rightBarButtonItem = nil;
这里“navBar”是XIB视图中NavigationBar的IBOutlet 在这里,我想隐藏按钮或根据某些条件显示它。所以我在“如果”测试条件,如果是真的,我将按钮设置为nil在viewDidLoad方法的目标视图。
这可能与你的问题不相关,但如果你想隐藏导航栏上的按钮,情况类似
将你的按钮保存在一个强outlet中(让我们称之为myButton),然后添加/删除它:
// Get the reference to the current toolbar buttons
NSMutableArray *toolbarButtons = [self.toolbarItems mutableCopy];
// This is how you remove the button from the toolbar and animate it
[toolbarButtons removeObject:self.myButton];
[self setToolbarItems:toolbarButtons animated:YES];
// This is how you add the button to the toolbar and animate it
if (![toolbarButtons containsObject:self.myButton]) {
// The following line adds the object to the end of the array.
// If you want to add the button somewhere else, use the `insertObject:atIndex:`
// method instead of the `addObject` method.
[toolbarButtons addObject:self.myButton];
[self setToolbarItems:toolbarButtons animated:YES];
}
因为它存储在输出中,所以即使它不在工具栏上,您也将保留对它的引用。
您需要操作工具栏。条目数组。
下面是我用来隐藏和显示Done按钮的一些代码。如果你的按钮在工具栏的极端边缘或在其他按钮之间,你的其他按钮将移动,所以如果你想让你的按钮只是消失,然后把你的按钮作为最后一个按钮的中心。我动画按钮移动的效果,我很喜欢它。
-(void)initLibraryToolbar {
libraryToolbarDocumentManagementEnabled = [NSMutableArray arrayWithCapacity:self.libraryToolbar.items.count];
libraryToolbarDocumentManagementDisabled = [NSMutableArray arrayWithCapacity:self.libraryToolbar.items.count];
[libraryToolbarDocumentManagementEnabled addObjectsFromArray:self.libraryToolbar.items];
[libraryToolbarDocumentManagementDisabled addObjectsFromArray:self.libraryToolbar.items];
trashCan = [libraryToolbarDocumentManagementDisabled objectAtIndex:3];
mail = [libraryToolbarDocumentManagementDisabled objectAtIndex:5];
[libraryToolbarDocumentManagementDisabled removeObjectAtIndex:1];
trashCan.enabled = NO;
mail.enabled = NO;
[self.libraryToolbar setItems:libraryToolbarDocumentManagementDisabled animated:NO];
}
所以现在可以使用下面的代码来显示您的按钮
[self.libraryToolbar setItems:libraryToolbarDocumentManagementEnabled animated:YES];
trashCan.enabled = YES;
mail.enabled = YES;
或者隐藏你的纽扣
[self.libraryToolbar setItems:libraryToolbarDocumentManagementDisabled animated:YES];
trashCan.enabled = NO;
mail.enabled = NO;
您可以很容易地获得视图并隐藏它
let view: UIView = barButtonItem.valueForKey("view") as! UIView
view.hidden = true