我在IB中创建了一个带有多个按钮的工具栏。我希望能够根据主窗口中数据的状态来隐藏/显示其中一个按钮。
UIBarButtonItem
没有隐藏属性,目前我找到的任何关于隐藏它们的示例都涉及将导航栏按钮设置为nil,在这里我认为不需要这样做,因为我可能需要再次显示按钮(更不用说,如果我将我的按钮连接到IBOutlet,如果我将其设置为nil,我不确定如何重新获取它)。
我在IB中创建了一个带有多个按钮的工具栏。我希望能够根据主窗口中数据的状态来隐藏/显示其中一个按钮。
UIBarButtonItem
没有隐藏属性,目前我找到的任何关于隐藏它们的示例都涉及将导航栏按钮设置为nil,在这里我认为不需要这样做,因为我可能需要再次显示按钮(更不用说,如果我将我的按钮连接到IBOutlet,如果我将其设置为nil,我不确定如何重新获取它)。
将你的按钮保存在一个strong插座中(我们称其为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];
}
因为它存储在插座中,所以即使它不在工具栏上,你仍将保留对它的引用。
我知道这个答案对于这个问题来说有些晚了,但如果其他人面临类似情况可能会有所帮助。
在iOS 7中,要隐藏一个条形按钮项,我们可以使用以下两种技术:
SetTitleTextAttributes
:这对于像“完成”、“保存”等按钮非常有效。然而,对于像添加、垃圾桶符号等物品(至少对我来说)它不起作用,因为它们不是文本。TintColor
:如果我有一个名为“deleteButton”的条形按钮项:要隐藏按钮,我使用了以下代码:
[self.deleteButton setEnabled:NO];
[self.deleteButton setTintColor: [UIColor clearColor]];
为了再次显示按钮,我使用了以下代码:-[self.deleteButton setEnabled:YES];
[self.deleteButton setTintColor:nil];
deleteButton.enabled = false; deleteButton.tintColor = UIColor.clearColor()
来禁用并隐藏按钮,使用 deleteButton.enabled = true; deleteButton.tintColor = nil
来重新启用并正常显示。 - UnixmonkeyUIControlState.disabled
设置了UIColor.clear
,并可以使用setEnabled
显示/隐藏按钮。当然,正如你所说,这仅适用于文本按钮。 - heyfrank这里是一个简单的方法:
hide: barbuttonItem.width = 0.01;
show: barbuttonItem.width = 0; //(0 defaults to normal button width, which is the width of the text)
我刚刚在我的视网膜iPad上运行了它,.01足够小,以至于它不会显示出来。
self.window?.tintColor = APP_PRIMARY_COLOR
设置了全局的色调颜色,则这将不起作用。 - Mehul Thakkar可以在不改变按钮宽度或从栏中删除按钮的情况下隐藏按钮。如果将样式设置为plain,删除标题并禁用按钮,则按钮会消失。要恢复它,只需撤销更改即可。
-(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;
}
}
以下是我的解决方案,虽然我是在寻找导航栏的解决方案时发现的。
navBar.topItem.rightBarButtonItem = nil;
这里,“navBar”是连接到XIB视图中NavigationBar的IBOutlet。我想根据某些条件隐藏或显示按钮。因此,我在“if”中测试了该条件,并且如果为真,则将按钮设置为nil并在目标视图的viewDidLoad方法中执行。
这可能与您的问题不完全相关,但如果您想隐藏NavigationBar上的按钮,可以参考类似的方法。
rightBarButtonItem
,请确保按钮项存储在一个强大的IBOutlet中,以便在将其从导航栏上取下时不会被释放。 - Nate对于 Swift 3 和 Swift 4,您可以执行以下操作来隐藏 UIBarButtomItem
:
self.deleteButton.isEnabled = false
self.deleteButton.tintColor = UIColor.clear
为了展示 UIBarButtonItem
:
self.deleteButton.isEnabled = true
self.deleteButton.tintColor = UIColor.blue
tintColor
必须指定您为UIBarButtomItem
使用的原始颜色。我目前正在运行OS X Yosemite Developer Preview 7和Xcode 6 beta 6,针对iOS 7.1,以下解决方案对我有效:
UINavigationItem
and UIBarButtonItem
sRun following code to remove
[self.navItem setRightBarButtonItem:nil];
[self.navItem setLeftBarButtonItem:nil];
Run following codes to add buttons again
[self.navItem setRightBarButtonItem:deleteItem];
[self.navItem setLeftBarButtonItem:addItem];
我在我的项目中使用了IBOutlets。所以我的解决方案是:
@IBOutlet weak var addBarButton: UIBarButtonItem!
addBarButton.enabled = false
addBarButton.tintColor = UIColor.clearColor()
当需要再次显示此栏时,只需设置反转属性即可。
在 Swift 3 中,使用 isEnable
属性而不是 enable
。
self.dismissButton.customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
iOS 8. 自定义图像的UIBarButtonItem。
尝试了许多不同的方法,大多数都没有帮助。
Max的解决方案,setTintColor
没有改变任何颜色。
我自己想出了这个方法,认为对某些人有用。
隐藏:
[self.navigationItem.rightBarButtonItem setEnabled:NO];
[self.navigationItem.rightBarButtonItem setImage:nil];
展示用途:
[self.navigationItem.rightBarButtonItem setEnabled:YES];
[self.navigationItem.rightBarButtonItem setImage:image];