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

Posted

技术标签:

【中文标题】隐藏/显示导航栏中两个右栏按钮之一【英文标题】:Hide/Show one of the two right bar buttons in the navigation bar 【发布时间】:2015-10-29 07:32:00 【问题描述】:

我使用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

 

【问题讨论】:

只需尝试 [shareButton.customView removeFromSuperview] 是否可以正常工作只是一个建议 【参考方案1】:

试试这个:

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

并调用上述方法并传递您要隐藏的栏按钮

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

【讨论】:

【参考方案2】:

本机 UIBarButtonItem 没有“.hidden”属性。

in this very related question 列出了许多可能的答案,我认为对您来说最好的答案可能是尝试子类化 UIBarButtonItem(HidableBarButtonItem 子类),如果项目应该是,则仅将标题替换为空字符串隐藏。

【讨论】:

【参考方案3】:

试试这个

-(UIBarButtonItem *)getLeftBarbuttonItem

  UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
  menuButton.frame = CGRectMake(0, 0, 24, 24);
  [menuButton setImage:[UIImage imageNamed:@"menu-icon.png"] forState:UIControlStateNormal];
  [menuButton addTarget:self.revealController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];

  UIBarButtonItem *leftbarButton = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
  return leftbarButton;

 

 -(void)showNavBarItem:(BOOL)isShow

  if(isShow)
      [self.navigationItem setLeftBarButtonItem:[self getLeftBarbuttonItem] animated:true];
  else
      [self.navigationItem setLeftBarButtonItem:nil animated:true];
   

 

右栏按钮隐藏

   -(UIBarButtonItem *)getFirstBarbuttonItem:(BOOL)isHide

   UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    menuButton.frame = CGRectMake(0, 0, 24, 24);
   [menuButton setImage:[UIImage imageNamed:@"menu-icon.png"] forState:UIControlStateNormal];
   [menuButton addTarget:self.revealController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
   menuButton.hidden = isHide;
   UIBarButtonItem *leftbarButton = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
return leftbarButton;
  

 -(UIBarButtonItem *)getSecondBarbuttonItem:(BOOL)isHide

    UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    menuButton.frame = CGRectMake(0, 0, 24, 24);
    [menuButton setImage:[UIImage imageNamed:@"menu-icon.png"] forState:UIControlStateNormal];
   [menuButton addTarget:self.revealController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
   menuButton.hidden = isHide;
   UIBarButtonItem *leftbarButton = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
   return leftbarButton;
  

  self.navigationItem.rightBarButtonItems = @[[self getFirstBarbuttonItem:YES],[self getSecondBarbuttonItem:NO]];

【讨论】:

如何隐藏self.navigationItem.rightBarButtonItems中的两个右栏按钮之一【参考方案4】:

对于 Swift 5.1

self.navigationItem.rightBarButtonItems?.remove(at: [indexOfButton])

This helped me

【讨论】:

以上是关于隐藏/显示导航栏中两个右栏按钮之一的主要内容,如果未能解决你的问题,请参考以下文章

在导航栏中添加彩色图像作为右栏按钮项

触摸时禁用其他栏按钮项:导航栏

如何在 iOS 的导航栏中添加标准信息按钮?

在导航栏折叠时隐藏的导航栏中显示图像

如何在导航栏中添加自定义视图?

将右栏按钮添加到导航栏的问题