More than 1 rightBarButtonItem on navigation bar

前端 未结 9 975
清歌不尽
清歌不尽 2021-01-30 02:06

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 anyb

相关标签:
9条回答
  • 2021-01-30 02:21

    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];
    
    0 讨论(0)
  • 2021-01-30 02:23

    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];
    
    0 讨论(0)
  • 2021-01-30 02:24

    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]];
    
    0 讨论(0)
  • 2021-01-30 02:31

    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];
    
    0 讨论(0)
  • 2021-01-30 02:32

    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.

    0 讨论(0)
  • 2021-01-30 02:34

    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.

    0 讨论(0)
提交回复
热议问题