1

I implemented two right bar button items on navigation bar in iOS9.0 using xcode7,I want to hide/show one right bar button with specified condition.I am using the following code.please help me

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

 }
IKKA
  • 6,297
  • 7
  • 50
  • 88
  • simply try [shareButton.customView removeFromSuperview] is it works fine just a suggestion – Arun Oct 29 '15 at 07:42

4 Answers4

1

Try this :

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

and call the above method and pass the bar button you want to hide

[self changeBarButtonVisibility:self.navigationItem.rightBarButtonItems[0] visibility:NO];
[self changeBarButtonVisibility:self.navigationItem.rightBarButtonItems[1] visibility:YES];
iOS Nepal
  • 103
  • 1
  • 10
0

There is no ".hidden" property to a native UIBarButtonItem.

There are a number of possible answers listed in this very related question, and I think the best one for you might be to try subclassing UIBarButtonItem (the HidableBarButtonItem subclass), only replacing the title with an empty string if the item is supposed to be hidden.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

Try this

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

 }

Right BarButton Hide

   -(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]];
DineshKumar
  • 1,641
  • 15
  • 13
  • how to hide one of the two right bar buttons in self.navigationItem.rightBarButtonItems – IKKA Oct 29 '15 at 08:26
0

For swift 5.1

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

This helped me

Apple_Magic
  • 477
  • 8
  • 26