How do I hide/show the right button in the Navigation Bar
Solution 1
Hide the button by setting the reference to nil, however if you want to restore it later, you'll need to hang onto a copy of it so you can reassign it.
UIBarButtonItem *oldButton = self.navigationItem.rightBarButtonItem;
[oldButton retain];
self.navigationItem.rightBarButtonItem = nil;
//... later
self.navigationItem.rightBarButtonItem = oldButton;
[oldButton release];
Personally, in my apps I make my nav buttons into @properties, so that I can trash & recreate them at will, so something like:
//mycontroller.h
UIBarButtonItem *rightNavButton;
@property (nonatomic, retain) UIBarButtonItem *rightNavButton;
//mycontroller.m
@synthesize rightNavButton;
- (UIBarButtonItem *)rightNavButton {
if (!rightNavButton) {
rightNavButton = [[UIBarButtonItem alloc] init];
//configure the button here
}
return rightNavButton;
}
//later, in your code to show/hide the button:
self.navigationItem.rightBarButtonItem = self.rightNavButton;
Solution 2
For Swift 3
if let button = self.navigationItem.rightBarButtonItem {
button.isEnabled = false
button.tintColor = UIColor.clear
}`
Solution 3
Show:
[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];
Hide:
[self.navigationItem.rightBarButtonItem.customView setAlpha:0.0];
You can even animate its showing/hiding
[UIView animateWithDuration:0.2 animations:^{
[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];
}];
Solution 4
Set reference to nil:
current_controller_in_navcontroller.navigationItem.rightBarButtonItem = nil;
Also be sure to call this in the controller currently shown by the navController, not for the navController itself.
Solution 5
If you have only one bar button item in the right side you can use this one,
self.navigationItem.rightBarButtonItem = nil;
Suppose if you have multiple bar button in the right side, for example suppose you have two bar button items(search button and filter button) in the right side of your navigation item. Now the right bar button items are
self.navigationItem.rightBarButtonItems = [searchItem,filterItem]
and you have to hide only search button, you can use like,
self.navigationItem.rightBarButtonItems = [filterItem]
Now what happening is, you can completely hide the search button from the navigation item and the filter item comes in the place of search item
Related videos on Youtube
Lauren Quantrell
Updated on August 01, 2020Comments
-
Lauren Quantrell over 3 years
I need to hide the right button in the Navigation Bar, then unhide it after the user selects some options.
Unfortunately, the following doesn't work:
NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES; // FOO CODE
Is there a way?
-
Lauren Quantrell about 13 yearsYour property method seems like the right solution. Thanks. Question: I'm not clear how I invoke: - (UIBarButtonItem *)rightNavButton {
-
Lauren Quantrell about 13 yearsSorry, another question. Is there any reason I shouldn't just recreate the button again after setting it to nil, rather than using the two methods you outline?
-
Stefan Ticu about 13 years@lauren-quantrell you should Accept the answer if it's correct, it will help the other users reading this post
-
Matt J about 13 yearsThe rightNavButton function is the "getter" part of the property, which I have manually declared here (the "setter" is handled by synthesize). You invoke it by calling self.rightNavButton.
-
Matt J about 13 yearsYou don't have to use the structure that I've used here, but I prefer it. As a matter of habit, anything that I'm going to need to reference at least twice somewhere in my controller logic, I create as a property. This allows me to lazy-load the object on-demand (but not before necessary) in one place. In addition, if this were, for example, a rather large UIImage, or a sound clip, and I encountered a low memory situation, I could set self.rightNavButton = nil to free up the memory, then the object would get regenerated the next time it was requested.
-
Lauren Quantrell about 13 yearsThanks. Thanks for helping out.
-
App Dev Guy over 9 yearsWhile this gives the illusion that it is hidden, it does not disable the functionality of it. I do believe that that is the desired effect.
-
Hardik Thakkar almost 9 yearsThis is not a proper solution.. if user click on that portion the button action will execute...
-
lenooh over 7 yearsit works, but it seems like you don't need the outlet.
-
arvidurs over 6 yearsI have to agree..this is not an answer.
-
Nguyễn Quang Tuấn over 4 yearsyour solution is so perfect :D
-
Saad Rehman over 2 yearsDoesn't work...
-
Amr Angry over 2 years@SaadRehman sorry i don't even remember which project that i was using this technique :( I am into swift more than Objective C i would like to help if you share a sample on GitHub so we can try something together if you wish
-
Saad Rehman over 2 yearsThanks man, I ended up making the button nil to hide it so its fine :)