Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationBar right button not showing up

I am using the following function via a notification to load a right button on my UINavigationBar, and even though I can trace out the button and verify it is allocated, it does not show up...any ideas?

EDIT 4/6/2011, 2:42PM

So, something interesting...the width always reports as 0.0...

- (void)showRightBarButton:(id)sender
{
    NSLog(@"Showing button");
    UIBarButtonItem *button = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                               target:self
                               action:@selector(showPI:)];
    [button setTitle:@"This Button"];
    self.navigationItem.rightBarButtonItem = button;
    //[[self.navigationItem rightBarButtonItem] setWidth:50];
    NSLog(@"Button width is %f.", self.navigationItem.rightBarButtonItem.width);
    [button release];
}
like image 965
diatrevolo Avatar asked Oct 12 '22 09:10

diatrevolo


1 Answers

[self.view setNeedsDisplay];

You're right. That line isn't needed. As far as the rest of the code goes I don't see what's wrong with it. The only thing I've come up with so far is that self isn't the currently displayed view controller or that you're missing a navigation controller. Perhaps you've created your UINavigationBar yourself instead of using a navigation controller?

Anyway, for easier debugging I would suggest the following:

- (void)showRightBarButton:(id)sender
{
    NSLog(@"Showing button");
    UIBarButtonItem *button = [[UIBarButtonItem alloc]
                             initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                  target:self
                                                  action:@selector(showPI:)];
    self.navigationItem.rightBarButtonItem = button;
    [button release];
}

EDIT: The width isn't interesting. It's always 0.0 unless you specify it yourself.

The problem is that you're adding the button in the wrong place. You're not supposed to add the button to the navigation item of the navigation controller, but to the navigation item of the controller that is currently displayed by the navigation controller.

like image 169
Erik B Avatar answered Oct 18 '22 01:10

Erik B