How do I hide/show the right button in the Navigation Bar

73,192

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

Share:
73,192

Related videos on Youtube

Lauren Quantrell
Author by

Lauren Quantrell

Updated on August 01, 2020

Comments

  • Lauren Quantrell
    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
    Lauren Quantrell about 13 years
    Your property method seems like the right solution. Thanks. Question: I'm not clear how I invoke: - (UIBarButtonItem *)rightNavButton {
  • Lauren Quantrell
    Lauren Quantrell about 13 years
    Sorry, 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
    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
    Matt J about 13 years
    The 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
    Matt J about 13 years
    You 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
    Lauren Quantrell about 13 years
    Thanks. Thanks for helping out.
  • App Dev Guy
    App Dev Guy over 9 years
    While 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
    Hardik Thakkar almost 9 years
    This is not a proper solution.. if user click on that portion the button action will execute...
  • lenooh
    lenooh over 7 years
    it works, but it seems like you don't need the outlet.
  • arvidurs
    arvidurs over 6 years
    I have to agree..this is not an answer.
  • Nguyễn Quang Tuấn
    Nguyễn Quang Tuấn over 4 years
    your solution is so perfect :D
  • Saad Rehman
    Saad Rehman over 2 years
    Doesn't work...
  • Amr Angry
    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
    Saad Rehman over 2 years
    Thanks man, I ended up making the button nil to hide it so its fine :)