iOS : Multiple Buttons on a Navigation Bar

In iOS 5 there is a new property to allow display of Back button in navigation bar AND other button on the right of this left button.
Before iOS 5, this was not allowed. So I found this post useful to display many button in the Navigation Bar even if we want the Back Button on the left.

Here is the code :

// create a toolbar to have two buttons in the right  
UIToolbar* tools = [[UIToolbar alloc] 
                     initWithFrame:CGRectMake(0, 0, 133, 44.01)]; 

// create the array to hold the buttons, which then gets added to the toolbar NSMutableArray* buttons = [[NSMutableArray alloc] 
                                         initWithCapacity:3]; 

// create a standard "add" button 
UIBarButtonItem* bi = [[UIBarButtonItem alloc] 
              initWithBarButtonSystemItem:UIBarButtonSystemItemAdd              target:self action:NULL]; 
bi.style = UIBarButtonItemStyleBordered; 
[buttons addObject:bi]; 
[bi release]; 

// create a spacer bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release]; 
// create a standard "refresh" button 
bi = [[UIBarButtonItem alloc] 
          initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh          target:self action:@selector(refresh:)]; 
bi.style = UIBarButtonItemStyleBordered; 
[buttons addObject:bi]; 
[bi release]; 

// stick the buttons in the toolbar 
[tools setItems:buttons animated:NO]; 
[buttons release]; 

// and put the toolbar in the nav bar 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]                                     initWithCustomView:tools]; 
[tools release];
This entry was posted in app development, iOS, iPhone and tagged , , , . Bookmark the permalink.