0

I try this code for the navigation bar

UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0,320, 70)];

and for the button I tried this code

UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(SaveButtonClicked:)];
self.navigationItem.rightBarButtonItem=add;

after this code i declare SaveButtonClicked method also.

When I try this code there is no error in this code and compiled successfully but the button is not show in the navigation bar.

Help me with right code and suggestions.

rmaddy
  • 314,917
  • 42
  • 532
  • 579

3 Answers3

0

1) You don't need to create a UINavigationBar. The navigation controller you're in provides that.

2) And if you did think you needed one, that probably means your view isn't in a navigation controller at all. Check what the value of self.navigationItem is. I imagine you'll find it to be nil. That means you need to redo your interface so this view is actually inside a navigation controller. "Embed in Navigation Controller" under "Editor" in Interface Builder may be of assistance there.

Alex Curylo
  • 4,744
  • 1
  • 27
  • 37
0

After you have created navigation bar

 UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0,320, 70)];

Then You need to create navigation item. and add navigation item to navigation bar

UINavigationItem *item = [[UINavigationItem alloc]init];
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(SaveButtonClicked:)];
item.rightBarButtonItem=add;
navbar.items = [NSArray arrayWithObjects:item, nil];
[self.view addSubview:navbar];

Hope this Helps!

saurabh goyal
  • 328
  • 4
  • 11
  • @surabh goyal...I tried your code and that work correctly but in navigation bar at a time only one item is displayed like button is displayed then title is not display and when title is display then button is not displayed .. plz help me with this issue. – Manan Choksi May 21 '16 at 18:35
  • Let me how are you adding both , but just by adding title in item in tne above code should do . item.title = @"Title"; – saurabh goyal May 22 '16 at 02:09
  • Thnks saurabh...I add your title code and I have both in display.. I am adding title with this code that's not working but your code work for me thnks. – Manan Choksi May 22 '16 at 05:14
0

you can just use this single line to achieve that:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveButtonClicked:)];

after this add this method

-(void)saveButtonClicked:(UIBarButtonItem *)sender{

}

for more you can refer Add button to navigationbar programmatically

Community
  • 1
  • 1
UdayM
  • 1,725
  • 16
  • 34
  • My button is display in the navigation bar but when button is display then title is not display and when title is display then button is not display. – Manan Choksi May 21 '16 at 19:59
  • @MananChoksi: where you are adding this line...self.navigationItem.rightbarButtonItem=------ – UdayM May 21 '16 at 23:10