Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a button to UINavigationBar?

How to add a button to UINavigationBar programmatically?

like image 821
RexOnRoids Avatar asked Oct 05 '22 23:10

RexOnRoids


People also ask

How do I add a button to my storyboard?

1. Add a button to the view by clicking the Object library button in the toolbar and dragging a button object onto the view. After placing the button onto the view, the button appears in the Document Outline as a subview. 2.


1 Answers

Sample code to set the rightbutton on a NavigationBar.

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
    style:UIBarButtonItemStyleDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[bar pushNavigationItem:item animated:NO];

But normally you would have a NavigationController, enabling you to write:

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
    style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
like image 293
Mads Mobæk Avatar answered Oct 09 '22 01:10

Mads Mobæk