如何隐藏 UINavigationBar Rightbarbutton 项目?

发布于 2024-11-07 14:21:06 字数 403 浏览 7 评论 0原文

我使用以下代码将信息按钮添加到导航栏:

UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    [infoButton addTarget:self action:@selector(showImage:) 
         forControlEvents:UIControlEventTouchUpInside]; 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];

现在我想根据某些条件在代码的某些部分隐藏此按钮。但我在导航栏中没有找到右侧栏按钮项目的任何隐藏属性?

I added the info button to the navigation bar using below code:

UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    [infoButton addTarget:self action:@selector(showImage:) 
         forControlEvents:UIControlEventTouchUpInside]; 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];

Now i want to hide this button at some part of the code based on some conditions. But i didn't find any hide property for right bar button item in the navigation bar?

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

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

发布评论

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

评论(4

总以为 2024-11-14 14:21:06

要隐藏,请尝试将 nil 分配给您的 rightBarButtonItem,如下所示。

self.navigationItem.rightBarButtonItem = nil ;

For making hidden, try with assigning nil to your rightBarButtonItem like below.

self.navigationItem.rightBarButtonItem = nil ;
请你别敷衍 2024-11-14 14:21:06

最好的选择是使用 buttonItem.enabled = NO 来指示该功能目前不可用。在大多数情况下,这应该是正确的行为。

但是,如果您想让它消失,最好的方法是存储对栏按钮的引用。当您希望 rightBarButtonItem 消失时,将其设置为 nil;当您希望其显示时,将其设置为存储的引用。

Best option is to use buttonItem.enabled = NO to indicate the functionality isn't available at the moment. That should, in most cases, be the right behavior.

However if you intend to make it disappear, the best way would be to store a reference to the bar button. Set the rightBarButtonItem to nil when you want it to disappear and set it to the stored reference when you want it to be displayed.

别挽留 2024-11-14 14:21:06

如果你有多个 UIBarButtonItems 并且你只想删除一个,你可以这样做:

NSMutableArray *barButtonItems = [self.toobbar.items mutableCopy];
[barButtonItems removeObject:self.buttonToRemove];
[self.toolbar setItems:[barButtonItems copy] animated:NO];

If you have multiple UIBarButtonItems and you just want to remove one, you can do this:

NSMutableArray *barButtonItems = [self.toobbar.items mutableCopy];
[barButtonItems removeObject:self.buttonToRemove];
[self.toolbar setItems:[barButtonItems copy] animated:NO];
一个人的夜不怕黑 2024-11-14 14:21:06

如果你只是想在视觉上“隐藏”它:

Swift 3:

self.navigationItem.rightBarButtonItem?.tintColor = UIColor.clear
self.navigationItem.rightBarButtonItem?.isEnabled = false

If you just want to 'hide' it visually:

Swift 3:

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