gdm
gdm

Reputation: 7948

Right UIBarButton not shown

As simple as this code is :

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Inizializzazione barra di navigazione
[[self navigationController] setNavigationBarHidden:NO];

UINavigationItem* a = [self navigationItem];
[a setTitle:@"SOME TITLE"];
UIImage *background = [UIImage imageNamed:@"header.png"];
CGSize newSize;
newSize.height=100;
[self.navigationController.navigationBar setBackgroundImage:background 
                                         forBarMetrics:UIBarMetricsDefault];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] 
                 initWithTitle:@"Homefgh"                                                                                  
                 style:UIBarButtonItemStyleBordered 
                 target:self action:@selector(home)];

self.navigationController.navigationItem.rightBarButtonItem = rightButton;
}

but the button is not shown (on the right). I tried also to put the code in the viewDidLoad. The viewWillAppear is inside a UIView pushed on top of the main UINavigationController. The strange thing is that the background image is correctly shown.

Upvotes: 0

Views: 43

Answers (4)

Dilip Manek
Dilip Manek

Reputation: 9143

Add this in your code

self.navigationItem.rightBarButtonItem = rightButton;

Instead of this code

self.navigationController.navigationItem.rightBarButtonItem = rightButton;

And it will be displayed.

Upvotes: 0

lakshmen
lakshmen

Reputation: 29084

- (void)viewDidLoad {
  [super viewDidLoad];

  UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];          
  self.navigationItem.rightBarButtonItem = anotherButton;
  [anotherButton release];
}

Remember to put it in viewDidLoad instead of viewWillAppear.

Upvotes: 0

brianLikeApple
brianLikeApple

Reputation: 4371

Changeself.navigationController.navigationItem.rightBarButtonItem = rightButton; to self.navigationItem.rightBarButtonItem = rightButton;

Upvotes: 0

Skyler
Skyler

Reputation: 2864

You should be able to do something as simple as this to get it to show up:

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] 
                 initWithTitle:@"Homefgh"                                                                                  
                 style:UIBarButtonItemStyleBordered 
                 target:self action:@selector(home)];

self.navigationItem.rightBarButtonItem = rightButton;

Upvotes: 3

Related Questions