如何隐藏 UIBarButtonItem?

发布于 2024-12-11 09:31:38 字数 352 浏览 0 评论 0原文

我在 IB 中创建了一个简单的 UI,它由一个 UINavigationBar 和一个我拖放到右侧的 UIBarButtonItem 组成。

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

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

self.NavigationItem.rightBarButton = nil;

...这对我不起作用。我还尝试创建和 IBOutlet 并将其链接到按钮,但是我也遇到了问题。我认为它应该非常简单,也许我把它弄得太复杂了,但在这一点上我很困惑!

请问有人可以帮我吗?

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.

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!

Please can someone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

愁杀 2024-12-18 09:31:38

UINavigationItem 没有 rightBarButton 属性。尝试 rightBarButtonItem 代替(或 [self.navigationItem setRightBarButtonItem:nil 动画:NO];):

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

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

self.navigationController.navigationItem.rightBarButtonItem = nil;
// Or
self.navigationItem.rightBarButtonItem = nil;
// Or
[self.navigationItem setRightBarButtonItem:nil animated:NO];
北渚 2024-12-18 09:31:38

只需重置按钮

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

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

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

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

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.

最冷一天 2024-12-18 09:31:38

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

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

揽清风入怀 2024-12-18 09:31:38

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

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

然后,当您想要隐藏栏按钮项目时,您可以这样做:

self.navigationItem.rightBarButton.enabled = NO;

它很蹩脚,没有隐藏属性,但这提供了相同的结果。

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.

[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.

月隐月明月朦胧 2024-12-18 09:31:38

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

[self.yourOutletRerence setImage: nil];

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

[self.yourOutletRerence setImage: nil];
看透却不说透 2024-12-18 09:31:38

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文