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!
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!
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];
Heres an example of how you can add two buttons as the rightbarbutton. The code below creates a segmentedcontrol containing up and down arrow buttons, which is then added as a customview for navigation's rightbarbutton:
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray array]]; [segmentedControl setMomentary:YES]; [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"icon-triangle-up.png"] atIndex:0 animated:NO]; [segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"icon-triangle-down.png"] atIndex:1 animated:NO]; segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth; segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; [segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged]; UIBarButtonItem * segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl]; self.navigationItem.rightBarButtonItem = segmentBarItem;
My suggestion would be to not implement the Add functionality as a button in the navigation bar. I assume that you're dealing with a table view of items below, so one way of handling this user interaction is to display an "Add new item" option as the last entry in your table view. This could be programmatically faded in when the user taps on the Edit button in your navigation bar by implementing the following delegate method:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; [self.tableView beginUpdates]; [self.tableView setEditing:editing animated:YES]; if (editing) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects count] inSection:0]; [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects count] inSection:0]; [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } [self.tableView endUpdates]; }
You then would need to make sure that the extra row is accounted for by increasing the count of rows using the following:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView.editing) return ([objects count] + 1); else return [objects count]; }
and then showing the green plus sign to its left, as opposed to the normal deletion editing style:
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone; if (indexPath.row >= [objects count]) return UITableViewCellEditingStyleInsert; else return UITableViewCellEditingStyleDelete; return UITableViewCellEditingStyleNone; }
Of course, you'll need to supply a name for it in your cellForRowAtIndexPath: implementation and handle its row selection, as well.
the navigation bar is a UIView so you can simply create a regulat UIButton and add it to your navigation bar as a subView.
Set the frame relative to the nav bar. If you want it to look exactly like the built in button, you will probably have to produce the graphics yourself as they are not exposed to the SDK AFAIK.
If you want to have two separated buttons as the rightBarButtonItem
you can do so by adding a UIToolbar
as custom view to the right bar item:
/************************************************* CREAT TWO RIGHT BAR BUTTON ITEMS *************************************************/ // create a toolbar to have two buttons in the right UIToolbar* customToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 44.01)]; // create the array to hold the buttons, which then gets added to the toolbar NSMutableArray* rightBarButtonArray = [[NSMutableArray alloc] initWithCapacity:2]; //Add the info button to the array UIButton* infoViewButton = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain]; [infoViewButton addTarget:self action:@selector(showInfoView) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] initWithCustomView:infoViewButton]; [rightBarButtonArray addObject:infoItem]; [infoItem release]; //Add the Done Button to the array UIBarButtonItem *bbi; bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(create:)]; [rightBarButtonArray addObject:bbi]; [bbi release]; //add the array to the custom toolbar [customToolbar setItems:rightBarButtonArray animated:NO]; [rightBarButtonArray release]; // and finally add the custom toolbar as custom view to the right bar item self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customToolbar]; [customToolbar release];
Here's the really easy, 2 line answer:
Step 1: Create a nib for the custom view w/ whatever content you want
Step 2: Add the nib to the toolbar as a custom view:
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"TwoButtonView" owner:self options:nil]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[subviewArray objectAtIndex:0]];
To show independent buttons (instead of segmented control buttons):
// 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]; // create a spacer bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; [buttons addObject:bi]; // create a standard "refresh" button bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)]; bi.style = UIBarButtonItemStyleBordered; [buttons addObject:bi]; // stick the buttons in the toolbar [tools setItems:buttons animated:NO]; // and put the toolbar in the nav bar self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
You can do it from Interface Builder. What I have done in my app - just added UIView on left Item of navigation bar and placed 3 buttons in this view and connected their action. You can follow this to add more than one button in left/right navigation item.
Refered: Toolbar items in sub-nib
if someone is passing by, here is the swift answer :
let barButton_array: [UIBarButtonItem] = [Button1, Button2] navigationItem.setRightBarButtonItems(barButton_array, animated: false)