9

I have created a simple UI in IB, this consists of a UINavigationBar and a UIBarButtonItem that I dragged and dropped on the right hand side.

I am trying to set this button to be hidden a certain times but I am having some problems.

So far I have tried using:

self.NavigationItem.rightBarButton = nil;

...which didn't work for me. I have also tried creating and IBOutlet and linking it to the button however I'm having problems with this too. I think it should be pretty simple and maybe I'm over-complicating it, but at this point I'm pretty stumped!

Please can someone help me out?

6 Answers6

29

UINavigationItem doesnt have a rightBarButton property. Try rightBarButtonItem instead (or [self.navigationItem setRightBarButtonItem:nil animated:NO];):

self.navigationController.navigationItem.rightBarButtonItem = nil;
// Or
self.navigationItem.rightBarButtonItem = nil;
// Or
[self.navigationItem setRightBarButtonItem:nil animated:NO];
chown
  • 51,908
  • 16
  • 134
  • 170
1

Just reset the buttons

   -(void)setItems:(NSArray *)items animated:(BOOL)animated 

More info here: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIToolbar_Class/Reference/Reference.html#//apple_ref/occ/instm/UIToolbar/setItems%3aanimated%3a

You can get the current items using the items property, then just remove the one you don't want to show and pass in the new NSArray.

Gabriel
  • 3,039
  • 6
  • 34
  • 44
0

You can also add a UIButton as the UIBarButtonItem's customView. Then set the hidden property on the customView (UIButton)

Nate Potter
  • 3,222
  • 2
  • 22
  • 24
0

Rather than deleting the bar button item and destroying the button and it's attached storyboard segue, you can just set it to clear text when it's disabled.

[self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor clearColor]}
                                                      forState:UIControlStateDisabled];

Then when ever you want the bar button item hidden, you can just do:

self.navigationItem.rightBarButton.enabled = NO;

It's lame there's no hidden property but this offers the same result.

puppybits
  • 1,110
  • 12
  • 16
0

Actually, you can just create an IBOutlet reference to the desired UIBarButtonItem and when needed just do as follow:

[self.yourOutletRerence setImage: nil];
Antonio Carlos
  • 770
  • 9
  • 19
0

The simplest solution: Just change the BarButtonItem's identifier to custom.