24

我想在导航栏上有两个 rightBarButtonItems。一个用于编辑,另一个用于添加。

显然我不能使用 Interface Builder 来实现它。

有人知道如何以编程方式进行吗?谢谢!

4

9 回答 9

68

这现在包含在 iOS 5 中,称为 rightBarButtonItems,注意复数

这是来自苹果文档的文本:

rightBarButtonItems

当接收者是顶部导航项时,一组自定义栏按钮项显示在导航栏的右侧。

@property(非原子,复制) NSArray *rightBarButtonItems

讨论


该数组可以包含 0 个或多个条形按钮项以显示在导航栏的右侧。项目按照它们在数组中出现的顺序从右到左显示。因此,数组中的第一项是最右边的项,其他项被添加到前一项的左侧。

如果没有足够的空间来显示数组中的所有项目,则不会显示与标题视图(如果存在)或栏左侧按钮重叠的项目

也可以使用 rightBarButtonItem 属性设置数组中的第一项。

在 UINavigationBar.h 中声明

以下是我在导航栏右侧实现搜索图标和编辑图标的方法:

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];
于 2012-02-04T19:25:31.237 回答
37

下面是一个如何添加两个按钮作为 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;
于 2009-09-01T13:38:53.910 回答
4

我的建议是不要将添加功能实现为导航栏中的按钮。我假设您正在处理下面项目的表格视图,因此处理此用户交互的一种方法是将“添加新项目”选项显示为表格视图中的最后一个条目。当用户通过实现以下委托方法点击导航栏中的编辑按钮时,这可以通过编程方式淡入:

- (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];
}

然后,您需要确保通过使用以下方法增加行数来计算额外的行:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    if (tableView.editing)
        return ([objects count] + 1);
    else
        return [objects count];     
}

然后在其左侧显示绿色加号,与正常的删除编辑样式相反:

- (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;
}

当然,您需要在 cellForRowAtIndexPath: 实现中为其提供名称并处理其行选择。

于 2009-05-31T21:50:25.233 回答
3

如果有人路过,这里是快速的答案:

let barButton_array: [UIBarButtonItem] = [Button1, Button2]
navigationItem.setRightBarButtonItems(barButton_array, animated: false)
于 2016-07-18T16:00:38.737 回答
2

这是非常简单的两行答案:

第 1 步:为自定义视图创建一个笔尖,包含您想要的任何内容

第 2 步:将 nib 作为自定义视图添加到工具栏:

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"TwoButtonView" owner:self options:nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[subviewArray objectAtIndex:0]];
于 2011-09-04T08:00:08.017 回答
2

导航栏是 UIView,因此您可以简单地创建一个规则 UIButton 并将其作为子视图添加到导航栏。

设置相对于导航栏的框架。如果您希望它看起来与内置按钮完全一样,您可能必须自己生成图形,因为它们不会暴露给 SDK AFAIK。

于 2009-05-31T07:59:55.473 回答
2

如果您想拥有两个单独的按钮,rightBarButtonItem您可以通过向UIToolbar右侧栏项目添加自定义视图来实现:

/*************************************************
       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];
于 2011-08-26T15:59:53.773 回答
0

您可以从 Interface Builder 中执行此操作。我在我的应用程序中所做的 - 只是在导航栏的左侧项目上添加了 UIView 并在此视图中放置了 3 个按钮并连接了它们的操作。您可以按照此在左/右导航项中添加多个按钮。

参考:子笔尖中的工具栏项目

在此处输入图像描述

于 2015-01-28T07:26:57.553 回答
0

显示独立按钮(而不是分段控制按钮):

// 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];
于 2012-02-27T08:44:31.183 回答