如何隐藏 UINavigationBar Rightbarbutton 项目?
我使用以下代码将信息按钮添加到导航栏:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要隐藏,请尝试将
nil
分配给您的rightBarButtonItem
,如下所示。For making hidden, try with assigning
nil
to yourrightBarButtonItem
like below.最好的选择是使用
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.
如果你有多个 UIBarButtonItems 并且你只想删除一个,你可以这样做:
If you have multiple UIBarButtonItems and you just want to remove one, you can do this:
如果你只是想在视觉上“隐藏”它:
Swift 3:
If you just want to 'hide' it visually:
Swift 3: