Reputation: 38162
I have added a right bar button item in my navigation item and want to remove this on some condition. This is what I am doing:
self.navigationItem.rightBarButtonItem = nil;
But not getting the desired behavior.
I want to hide it but do not find any method for it.
Upvotes: 32
Views: 29601
Reputation: 13527
Swift 5/4/3 - I have a couple of buttons on both left and right side of navigation bar so I hide them using:
func hideNavItems() {
navigationItem.setLeftBarButtonItems(nil, animated: true)
navigationItem.setRightBarButtonItems(nil, animated: true)
}
In my case I actually need to show those buttons again at later point so I keep them in an array:
var leftNavItems: [UIBarButtonItem]!
var rightNavItems: [UIBarButtonItem]!
and then I just call a function to show (re-add) them:
func showNavItems() {
navigationItem.setLeftBarButtonItems(leftNavItems, animated: true)
navigationItem.setRightBarButtonItems(rightNavItems, animated: true)
}
Upvotes: 11
Reputation: 2287
For others: I also tried
self.navigationItem.rightBarButtonItem = nil;
and it did not work, then I notice I did not call this from the main thread.
Upvotes: 0
Reputation: 35392
if let nav = self.navigationController {
nav.navigationItem.setRightBarButtonItems(nil, animated: true)
nav.navigationBar.topItem?.rightBarButtonItems = []
}
Upvotes: 0
Reputation: 2374
It worked for me with the following code:
self.navigationItem?.rightBarButtonItem = nil
navigationBar.items = [self.navigationItem!]
Upvotes: 0
Reputation: 1333
i know three ways: (supossing in right side)
if you have more than one bar button:
self.navigationItem.rightBarButtonItems = nil
else if you have only one bar button
self.navigationItem.rightBarButtonItem = nil
set a nil button :
self.navigationItem.setRightBarButtonItem(nil, animated: false)
Upvotes: 9
Reputation: 11
Recently I am facing the same issue and I solved it (thanks to Erik B., your answer inspired me! unfortunately i can't vote your answer yet)
I think my right bar button can't vanish because I called it in viewWillAppear
function, and at that function, self
isn't referred to current UIViewController
.
This worked for me, instead of setting the button in viewWillAppear
, I set it in navigationController:willShowViewController:animated:
function at previous UIViewController
.
For example, I call SecondViewController
from FirstViewController
and I want to hide right bar button in SecondViewController
, put this in FirstViewController.m
:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if ([viewController isKindOfClass:[SecondViewController class]]) {
[viewController.navigationItem setRightBarButtonItem:nil];
}
}
Upvotes: 1
Reputation: 362
I've had this issue when adding a navigation bar to a view controller that isn't actually embedded in a navigation controller. In that case the self.navigationItem isn't the right object for holding your bar buttons. What I found works (although you should probably just embed the viewcontroller in a navigation controller) is to create a property for the navigation bar you added in Interface Builder and link it up as normal. In this case I've named it "navBar".
Then you have to get the navBar's navigation item instead of the view's navigation item. In which case this code seems to work:
UINavigationItem *item = [self.navBar.items objectAtIndex:0];
item.rightBarButtonItem = nil;
Upvotes: 0
Reputation: 8100
My answer is not pertaining specifically to this question but as this is the top result on google hence I will contribute my experience with the problem
I had used an array of items for rightBarButtonItem,hence somehow using
self.navigationItem.rightBarButtonItem = nil;
wasn't working for me so I made an empty barbutton item and set it as rightBarButtonItem using
[self.navigationItem setRightBarButtonItems:@[self.clearRightButtonItem]];
and now its gone.
Upvotes: 1
Reputation: 4140
I solved it by create a new rightBarButtonItem.
Create property:
@property (nonatomic, strong) UIBarButtonItem *clearRightButtonItem;
Synthesize:
@synthesize clearRightButtonItem = _clearRightButtonItem;
Create getter:
- (UIBarButtonItem *) clearRightButtonItem {
if (_clearRightButtonItem == nil) {
UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 38, 38)];
[rightButton setImage:nil forState:UIControlStateNormal];
_clearRightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}
return _clearRightButtonItem;
}
And then set rightBarButtonItem with new clearRightButtonItem:
self.rightButtonItem = self.clearRightButtonItem;
Upvotes: 2
Reputation: 42574
What you're doing should work. I've done that lots of times. Are you sure you're removing the button from the correct navigation item? Is self
the currently displayed UIViewController
?
Upvotes: 21