Abhinav
Abhinav

Reputation: 38162

Removing right bar button item from navigation item

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

Answers (10)

budiDino
budiDino

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

user1105951
user1105951

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

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

Swift 3/4:

if let nav = self.navigationController {
   nav.navigationItem.setRightBarButtonItems(nil, animated: true)
   nav.navigationBar.topItem?.rightBarButtonItems = []
}

Upvotes: 0

sahiljain
sahiljain

Reputation: 2374

It worked for me with the following code:

self.navigationItem?.rightBarButtonItem = nil
navigationBar.items = [self.navigationItem!]

Upvotes: 0

Jirson Tavera
Jirson Tavera

Reputation: 1333

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)

Upvotes: 9

algazel
algazel

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

Jake Hargus
Jake Hargus

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

Abhinav Singh
Abhinav Singh

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

edzio27
edzio27

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

Erik B
Erik B

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

Related Questions