如何隐藏UIBarButtonItem?

时间:2022-11-25 19:29:01

I have created a simple UI in IB, this consists of a UINavigationBar and a UIBarButtonItem that I dragged and dropped on the right hand side.

我在IB中创建了一个简单的UI,它包含一个UINavigationBar和一个UIBarButtonItem,我在右侧拖放。

I am trying to set this button to be hidden a certain times but I am having some problems.

我试图将此按钮设置为隐藏一定时间,但我遇到了一些问题。

So far I have tried using:

到目前为止,我尝试过使用:

self.NavigationItem.rightBarButton = nil;

...which didn't work for me. I have also tried creating and IBOutlet and linking it to the button however I'm having problems with this too. I think it should be pretty simple and maybe I'm over-complicating it, but at this point I'm pretty stumped!

......这对我不起作用。我也尝试过创建和IBOutlet并将其链接到按钮,但我也遇到了问题。我认为这应该很简单,也许我过于复杂了,但此时我很难过!

Please can someone help me out?

请有人帮帮我吗?

6 个解决方案

#1


25  

UINavigationItem doesnt have a rightBarButton property. Try rightBarButtonItem instead (or [self.navigationItem setRightBarButtonItem:nil animated:NO];):

UINavigationItem没有rightBarButton属性。请尝试使用rightBarButtonItem(或[self.navigationItem setRightBarButtonItem:nil animated:NO];):

self.navigationController.navigationItem.rightBarButtonItem = nil;
// Or
self.navigationItem.rightBarButtonItem = nil;
// Or
[self.navigationItem setRightBarButtonItem:nil animated:NO];

#2


1  

Just reset the buttons

只需重置按钮即可

   -(void)setItems:(NSArray *)items animated:(BOOL)animated 

More info here: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIToolbar_Class/Reference/Reference.html#//apple_ref/occ/instm/UIToolbar/setItems%3aanimated%3a

更多信息:http://developer.apple.com/iphone/library/documentation/uikit/reference/UIToolbar_Class/Reference/Reference.html#//apple_ref/occ/instm/UIToolbar/setItems%3aanimated%3a

You can get the current items using the items property, then just remove the one you don't want to show and pass in the new NSArray.

您可以使用items属性获取当前项目,然后只删除您不想显示的项目并传入新的NSArray。

#3


0  

You can also add a UIButton as the UIBarButtonItem's customView. Then set the hidden property on the customView (UIButton)

您还可以添加UIButton作为UIBarButtonItem的customView。然后在customView(UIButton)上设置隐藏属性

#4


0  

Rather than deleting the bar button item and destroying the button and it's attached storyboard segue, you can just set it to clear text when it's disabled.

而不是删除栏按钮项并销毁按钮及其附加的故事板segue,您可以将其设置为在禁用时清除文本。

[self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor clearColor]}
                                                      forState:UIControlStateDisabled];

Then when ever you want the bar button item hidden, you can just do:

然后,当你想要隐藏条形按钮项时,你可以这样做:

self.navigationItem.rightBarButton.enabled = NO;

It's lame there's no hidden property but this offers the same result.

这是蹩脚的,没有隐藏的财产,但这提供了相同的结果。

#5


0  

Actually, you can just create an IBOutlet reference to the desired UIBarButtonItem and when needed just do as follow:

实际上,您可以创建一个对所需UIBarButtonItem的IBOutlet引用,并在需要时执行以下操作:

[self.yourOutletRerence setImage: nil];

#6


0  

The simplest solution: Just change the BarButtonItem's identifier to custom.

最简单的解决方案:只需将BarButtonItem的标识符更改为自定义。

#1


25  

UINavigationItem doesnt have a rightBarButton property. Try rightBarButtonItem instead (or [self.navigationItem setRightBarButtonItem:nil animated:NO];):

UINavigationItem没有rightBarButton属性。请尝试使用rightBarButtonItem(或[self.navigationItem setRightBarButtonItem:nil animated:NO];):

self.navigationController.navigationItem.rightBarButtonItem = nil;
// Or
self.navigationItem.rightBarButtonItem = nil;
// Or
[self.navigationItem setRightBarButtonItem:nil animated:NO];

#2


1  

Just reset the buttons

只需重置按钮即可

   -(void)setItems:(NSArray *)items animated:(BOOL)animated 

More info here: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIToolbar_Class/Reference/Reference.html#//apple_ref/occ/instm/UIToolbar/setItems%3aanimated%3a

更多信息:http://developer.apple.com/iphone/library/documentation/uikit/reference/UIToolbar_Class/Reference/Reference.html#//apple_ref/occ/instm/UIToolbar/setItems%3aanimated%3a

You can get the current items using the items property, then just remove the one you don't want to show and pass in the new NSArray.

您可以使用items属性获取当前项目,然后只删除您不想显示的项目并传入新的NSArray。

#3


0  

You can also add a UIButton as the UIBarButtonItem's customView. Then set the hidden property on the customView (UIButton)

您还可以添加UIButton作为UIBarButtonItem的customView。然后在customView(UIButton)上设置隐藏属性

#4


0  

Rather than deleting the bar button item and destroying the button and it's attached storyboard segue, you can just set it to clear text when it's disabled.

而不是删除栏按钮项并销毁按钮及其附加的故事板segue,您可以将其设置为在禁用时清除文本。

[self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor clearColor]}
                                                      forState:UIControlStateDisabled];

Then when ever you want the bar button item hidden, you can just do:

然后,当你想要隐藏条形按钮项时,你可以这样做:

self.navigationItem.rightBarButton.enabled = NO;

It's lame there's no hidden property but this offers the same result.

这是蹩脚的,没有隐藏的财产,但这提供了相同的结果。

#5


0  

Actually, you can just create an IBOutlet reference to the desired UIBarButtonItem and when needed just do as follow:

实际上,您可以创建一个对所需UIBarButtonItem的IBOutlet引用,并在需要时执行以下操作:

[self.yourOutletRerence setImage: nil];

#6


0  

The simplest solution: Just change the BarButtonItem's identifier to custom.

最简单的解决方案:只需将BarButtonItem的标识符更改为自定义。