Reputation: 6471
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
}
Upvotes: 1
Views: 952
Reputation: 477
For swift 5.1
self.navigationItem.rightBarButtonItems?.remove(at: [indexOfButton])
Upvotes: 0
Reputation: 103
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];
Upvotes: 1
Reputation: 1689
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]];
Upvotes: 0
Reputation: 89509
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.
Upvotes: 0