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

时间:2015-10-29 07:32:01

标签: 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

 }

4 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

没有" .hidden"属性为原生UIBarButtonItem。

列出了许多可能的答案in this very related question,我认为最好的答案可能是尝试子类化UIBarButtonItem(HidableBarButtonItem子类),如果项目是假设的话,只用空字符串替换标题被隐藏。

答案 2 :(得分:0)

试试这个

-(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];
   }

 }

右BarButton隐藏

   -(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]];

答案 3 :(得分:0)

快速5.1

<form method="post" id="IdA" action="https://example.com/action.php"><input id='valid' name='valid' value='012.23945765955' type="hidden"><button class="g-recaptcha" data-sitekey="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" data-callback="onSubmit" id="IdA" style="background:url(https://www.example.com/button.gif);width:190px;height:58px;border:none;cursor:pointer;display:none;" type="submit"></button></form>

This helped me