ios 如何显示/隐藏 UIBarButtonItem?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10021748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I show/hide a UIBarButtonItem?
提问by Sasha
I created a toolbar in IB with several buttons. I would like to be able to hide/show one of the buttons depending on the state of the data in the main window.
我在 IB 中创建了一个带有几个按钮的工具栏。我希望能够根据主窗口中的数据状态隐藏/显示按钮之一。
UIBarButtonItem
doesn't have a hidden property, and any examples I've found so far for hiding them involve setting nav bar buttons to nil, which I don't think I want to do here because I may need to show the button again (not to mention that, if I connect my button to an IBOutlet, if I set that to nil I'm not sure how I'd get it back).
UIBarButtonItem
没有隐藏属性,到目前为止我发现的任何隐藏它们的示例都涉及将导航栏按钮设置为零,我认为我不想在这里这样做,因为我可能需要再次显示按钮(不是值得一提的是,如果我将我的按钮连接到 IBOutlet,如果我将其设置为 nil,我不确定如何将其取回)。
回答by lnafziger
Save your button in a strongoutlet (let's call it myButton
) and do this to add/remove it:
将您的按钮保存在一个强大的插座中(我们称之为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];
}
Because it is stored in the outlet, you will keep a reference to it even when it isn't on the toolbar.
因为它存储在插座中,即使它不在工具栏上,您也会保留对它的引用。
回答by Max
I know this answer is late for this question. However, it might help if anybody else faces a similar situation.
我知道这个问题的答案迟到了。但是,如果其他人面临类似情况,这可能会有所帮助。
In iOS 7, to hide a bar button item, we can use the following two techniques :-
在 iOS 7 中,要隐藏条形按钮项,我们可以使用以下两种技术:-
- use
SetTitleTextAttributes
:- This works great on bar button items like "Done", "Save" etc. However, it does not work on items like Add, Trash symbol etc.(atleast not for me) since they are not texts. - use
TintColor
:- If I have a bar button item called "deleteButton" :-
- 使用
SetTitleTextAttributes
:- 这对“完成”、“保存”等栏按钮项目非常有效。但是,它不适用于添加、垃圾箱符号等项目(至少不适合我),因为它们不是文本。 - 使用
TintColor
:-如果我有一个名为“deleteButton”的条形按钮项目:-
To hide the button, I used the following code:-
为了隐藏按钮,我使用了以下代码:-
[self.deleteButton setEnabled:NO];
[self.deleteButton setTintColor: [UIColor clearColor]];
To show the button again I used the following code:-
为了再次显示按钮,我使用了以下代码:-
[self.deleteButton setEnabled:YES];
[self.deleteButton setTintColor:nil];
回答by Drew Rosenberg
Here's a simple approach:
这是一个简单的方法:
hide: barbuttonItem.width = 0.01;
show: barbuttonItem.width = 0; //(0 defaults to normal button width, which is the width of the text)
I just ran it on my retina iPad, and .01 is small enough for it to not show up.
我只是在我的视网膜 iPad 上运行它,0.01 小到它不会出现。
回答by Eli Burke
It is possible to hide a button in place without changing its width or removing it from the bar. If you set the style to plain, remove the title, and disable the button, it will disappear. To restore it, just reverse your changes.
可以在不改变其宽度或将其从栏中移除的情况下将按钮隐藏到位。如果您将样式设置为普通,删除标题,并禁用按钮,它就会消失。要恢复它,只需撤销您的更改。
-(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;
}
}
回答by vishal dharankar
Below is my solution though i was looking it for Navigation Bar.
以下是我的解决方案,尽管我正在寻找导航栏。
navBar.topItem.rightBarButtonItem = nil;
Here "navBar" is a IBOutlet to the NavigationBar in the view in XIB Here i wanted to hide the button or show it based on some condition. So i m testing for the condition in "If" and if true i am setting the button to nil in viewDidLoad method of the target view.
这里的“导航栏”是一个IBOutlet中的导航栏在厦门国际银行的观点在这里,我想隐藏的按钮或显示它基于一些条件。因此,我正在测试“If”中的条件,如果为真,我将在目标视图的 viewDidLoad 方法中将按钮设置为 nil。
This may not be relevant to your problem exactly but something similar incase if you want to hide buttons on NavigationBar
这可能与您的问题不完全相关,但如果您想隐藏 NavigationBar 上的按钮,则有类似的情况
回答by pableiros
For Swift 3 and Swift 4 you can do this to hide the UIBarButtomItem
:
对于 Swift 3 和 Swift 4,您可以这样做来隐藏UIBarButtomItem
:
self.deleteButton.isEnabled = false
self.deleteButton.tintColor = UIColor.clear
And to show the UIBarButtonItem
:
并显示UIBarButtonItem
:
self.deleteButton.isEnabled = true
self.deleteButton.tintColor = UIColor.blue
On the tintColor
you must have to specify the origin color you are using for the UIBarButtomItem
在 上,tintColor
您必须指定用于UIBarButtomItem
回答by Olcay Erta?
I am currently running OS X Yosemite Developer Preview 7 and Xcode 6 beta 6 targeting iOS 7.1 and following solution works fine for me:
我目前正在运行针对 iOS 7.1 的 OS X Yosemite Developer Preview 7 和 Xcode 6 beta 6,以下解决方案对我来说很好用:
- Create outlet for
UINavigationItem
andUIBarButtonItem
s Run 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];
- 为
UINavigationItem
和UIBarButtonItem
s创建出口 运行以下代码删除
[self.navItem setRightBarButtonItem:nil]; [self.navItem setLeftBarButtonItem:nil];
运行以下代码再次添加按钮
[self.navItem setRightBarButtonItem:deleteItem]; [self.navItem setLeftBarButtonItem:addItem];
回答by Den
I used IBOutlets in my project. So my solution was:
我在我的项目中使用了 IBOutlets。所以我的解决方案是:
@IBOutlet weak var addBarButton: UIBarButtonItem!
addBarButton.enabled = false
addBarButton.tintColor = UIColor.clearColor()
And when you'll need to show this bar again, just set reversed properties.
当您需要再次显示此栏时,只需设置反转属性即可。
In Swift 3instead enable
use isEnable
property.
在Swift 3 中改为enable
使用isEnable
属性。
回答by Dmitry Semenyuk
self.dismissButton.customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
self.dismissButton.customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
回答by Rinto Rapheal
iOS 8. UIBarButtonItem with custom image.
Tried many different ways, most of them were not helping.
Max's solution, thesetTintColor
was not changing to any color.
I figured out this one myself, thought it will be of use to some one.
iOS 8. 带有自定义图像的 UIBarButtonItem。尝试了很多不同的方法,大多数都没有帮助。Max的解决方案,setTintColor
没有改变任何颜色。我自己想出了这个,认为它会对某些人有用。
For Hiding:
隐藏:
[self.navigationItem.rightBarButtonItem setEnabled:NO];
[self.navigationItem.rightBarButtonItem setImage:nil];
For Showing:
用于展示:
[self.navigationItem.rightBarButtonItem setEnabled:YES];
[self.navigationItem.rightBarButtonItem setImage:image];