32

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.

Abhinav
  • 37,684
  • 43
  • 191
  • 309

10 Answers10

21

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?

Erik B
  • 40,889
  • 25
  • 119
  • 135
  • I even had reference to my UIBarButton item held in a property, and setting the property to nil didn't work. It had to be done like this. – abc123 Oct 15 '13 at 03:03
  • 1
    If you have multiple left items (a transparent spacer and an item in my case) navigationItem.leftBarButtonItems = nil will work – vokilam Nov 20 '13 at 08:27
11

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)
}
budiDino
  • 13,044
  • 8
  • 95
  • 91
  • u can follow this answer. its working. hiding and showing can't be done directly in nav bar. So go with this approach. – Bilal Khan Aug 17 '23 at 08:05
9

i know three ways: (supossing in right side)

  1. if you have more than one bar button:
    self.navigationItem.rightBarButtonItems = nil

  2. else if you have only one bar button
    self.navigationItem.rightBarButtonItem = nil

  3. set a nil button :
    self.navigationItem.setRightBarButtonItem(nil, animated: false)

Jirson Tavera
  • 1,303
  • 14
  • 18
2

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;
edzio27
  • 4,126
  • 7
  • 33
  • 48
1

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.

Abhinav Singh
  • 7,880
  • 1
  • 23
  • 33
1

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];
    }
}
algazel
  • 11
  • 1
0

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;
Jake Hargus
  • 352
  • 3
  • 11
0

It worked for me with the following code:

self.navigationItem?.rightBarButtonItem = nil
navigationBar.items = [self.navigationItem!]
sahiljain
  • 2,215
  • 1
  • 29
  • 39
0

Swift 3/4:

if let nav = self.navigationController {
   nav.navigationItem.setRightBarButtonItems(nil, animated: true)
   nav.navigationBar.topItem?.rightBarButtonItems = []
}
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
0

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.

user1105951
  • 2,259
  • 2
  • 34
  • 55