More than 1 rightBarButtonItem on navigation bar

前端 未结 9 980
清歌不尽
清歌不尽 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];
    

提交回复
热议问题