ios - 隐藏/显示导航栏中两个右侧栏按钮之一

标签 ios objective-c iphone xcode7 uibarbuttonitem

我使用xcode7在iOS9.0的导航栏上实现了两个右栏按钮项,我想在指定条件下隐藏/显示一个右栏按钮。我正在使用以下代码。请帮助我

UIBarButtonItem *selectButton= [[UIBarButtonItem alloc]initWithTitle:@"Select" style:UIBarButtonItemStylePlain target:self action:@selector(clickOnSelect:)];

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]initWithTitle:@"Share" style:UIBarButtonItemStylePlain target:self action:@selector(clickOnShare:)];

self.navigationItem.rightBarButtonItems = @[selectButton,shareButton];

-(void)clickOnSelect:(id)sender{

 NSLog(@"self.navigationItem.rightBarButtonItems  :%@",self.navigationItem.rightBarButtonItems);        
 UIBarButtonItem *shareButton = (UIBarButtonItem *) [self.navigationItem.rightBarButtonItems objectAtIndex:1];  
 shareButton.customView.hidden = YES; // its not working

 }

最佳答案

试试这个:

-(void) changeBarButtonVisibility:(UIBarButtonItem*) barButtonItem visibility:(BOOL) shouldShow {
    UIColor *tintColor = shouldShow == NO ? [UIColor clearColor] : nil;
    [barButtonItem setEnabled:shouldShow];
    [barButtonItem setTintColor:tintColor];
}

然后调用上面的方法,传入你要隐藏的bar button

[self changeBarButtonVisibility:self.navigationItem.rightBarButtonItems[0] visibility:NO];
[self changeBarButtonVisibility:self.navigationItem.rightBarButtonItems[1] visibility:YES];

关于ios - 隐藏/显示导航栏中两个右侧栏按钮之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33408305/

相关文章:

ios - 如果快速触发按钮上的长按手势,如何获取按钮文本?

ios - 隐藏 UIButton 直到达到特定的点数

iphone - 显示 UIAlertView 会导致 EXC_BAD_ACCESS

iphone - 未收到从 MFMailComposer 发送的附件

android - 文本对齐不会在 native react 中自动对齐

ios - UIImageView 内容模式

iphone - 如何在 iPhone 横向模式下删除 UITableview 滚动中的空白?

iphone - 如何在 iPhone 内存中保存 pdf 文件并使其可通过使用钛的 pdf 文件打开器应用程序访问

iphone - 为什么我不能更改 UIBarButtonItem 的标题?

ios - 设置 : Cannot Perform Set On Containers