Hide the rightBarButtonItem of a navigation controller
Asked Answered
C

3

18

Does anyone know how to hide a rightBarButtonItem of a UINavigationController? In my application, I have an edit button as a rightBarButtonItem of a UINavigationController. I want to hide this ? UIBarButton` when some operations are done.

Connection answered 15/6, 2010 at 5:49 Comment(1)
self.navigationItem.rightBarButtonItems[1] setEnabled:NO]; if you have an array, like if you use flexible space.Eventual
G
19

To Hide the right button: self.navigationItem.rightBarButtonItem = nil;

Now, to show it:

  1. If you setup the right button in your view controller by assigning it to self.editButtonItem then simply assign it again in order to show it:

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

  2. If you setup the right button in your view controller by allocating and initing a UIBarButtonItem, then simply keep a reference to the UIBarButtonItem in your view controller, and assign it again when you need to show it.

Galactic answered 12/11, 2011 at 17:44 Comment(1)
I tried assigning rightBarButtonItem to nil in Objective C it doesn't work for meThorbert
W
18

Try

self.navigationItem.rightBarButtonItem = nil;

When you want it back though you will have to instanciate a button i.e.

UIBarButtonItem *rightBarButton = 
 [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                               target:self
                                               action:@selector(searchBar:)];
self.navigationItem.rightBarButtonItem = rightBarButton;
[rightBarButton release];
Worsen answered 15/6, 2010 at 6:6 Comment(2)
what if i dont want to recreate object? for example, i want to hide rightBarButton to wide frame of searchBar on titleView of navigationBar. i mean, does hiding is possible by changing property or something?Sylvie
if you would like to subclass UINavigationController. you can do: self.topViewController.navigationItem.rightBarButtonItem = rightBarButton; self is your UINavigationController custom classGlochidiate
P
17

If you need to hide/show the button based on some condition, try this:

if (condition) { 
    self.navigationItem.rightBarButtonItem.title = @"";
    self.navigationItem.rightBarButtonItem.enabled = NO;
} else {
    self.navigationItem.rightBarButtonItem.title = @"my button title";
    self.navigationItem.rightBarButtonItem.enabled = YES;
}

This way you don't have to save a reference to the button in a property or worry about wiring up the action on a new button.

Precambrian answered 24/3, 2014 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.