Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More than 1 rightBarButtonItem on navigation bar

I would like to have two rightBarButtonItems on navigation bar. One for Edit and the other for Add.

Obviously I can't make it using Interface Builder.

Does anybody know how to do it programmatically? Thanks!


1 Answers

This is now included in iOS 5 and is called rightBarButtonItems, notice the plural

Here is the text from the apple docs:

rightBarButtonItems

An array of custom bar button items to display on the right side of the navigation bar when the receiver is the top navigation item.

@property(nonatomic, copy) NSArray *rightBarButtonItems

Discussion

This array can contain 0 or more bar button items to display on the right side of the
navigation bar. Items are displayed right-to-left in the same order as they appear in the array. Thus, the first item in the array is the rightmost item and other items are added to the left of the previous item.

If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the left side of the bar are not
displayed.

The first item in the array can also be set using the rightBarButtonItem property.

Declared In UINavigationBar.h

Here is how I implemented a Search icon and an Edit icon on the right side of the nav bar:

UIBarButtonItem *searchButton         = [[UIBarButtonItem alloc]
                                         initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                         target:self
                                         action:@selector(searchItem:)];

UIBarButtonItem *editButton          = [[UIBarButtonItem alloc] 
                                         initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
                                         target:self action:@selector(editItem:)];

self.navigationItem.rightBarButtonItems =
[NSArray arrayWithObjects:editButton, searchButton, nil];
like image 96
jroyce Avatar answered Sep 14 '25 03:09

jroyce