Iphone 导航栏上有多个RightBarButtonim
我想在导航栏上有两个右按钮。一个用于编辑,另一个用于添加 显然,我不能使用Interface BuilderIphone 导航栏上有多个RightBarButtonim,iphone,navigation,Iphone,Navigation,我想在导航栏上有两个右按钮。一个用于编辑,另一个用于添加 显然,我不能使用Interface Builder 有人知道如何通过编程实现吗?谢谢 导航栏是UIView,因此您只需创建regulat UIButton并将其作为子视图添加到导航栏中即可 相对于导航栏设置帧。如果您希望它看起来与内置按钮完全相同,您可能必须自己生成图形,因为它们没有暴露在SDK AFAIK中。我的建议是不要将添加功能作为导航栏中的按钮来实现。我假设您正在处理下面项目的表视图,因此处理此用户交互的一种方法是将“添加新项目”
有人知道如何通过编程实现吗?谢谢 导航栏是UIView,因此您只需创建regulat UIButton并将其作为子视图添加到导航栏中即可
相对于导航栏设置帧。如果您希望它看起来与内置按钮完全相同,您可能必须自己生成图形,因为它们没有暴露在SDK AFAIK中。我的建议是不要将添加功能作为导航栏中的按钮来实现。我假设您正在处理下面项目的表视图,因此处理此用户交互的一种方法是将“添加新项目”选项显示为表视图中的最后一项。当用户通过实现以下委托方法点击导航栏中的“编辑”按钮时,这可能会以编程方式淡入:
- (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:implementation中为它提供一个名称,并处理它的行选择。下面是一个示例,说明如何添加两个按钮作为rightbarbutton。下面的代码创建了一个包含上下箭头按钮的分段控件,然后将其添加为导航的rightbarbutton的customview:
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;
如果要将两个单独的按钮用作
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];
下面是一个非常简单的两行答案: 步骤1:为自定义视图创建一个nib,包含您想要的任何内容 步骤2:将笔尖作为自定义视图添加到工具栏:
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"TwoButtonView" owner:self options:nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[subviewArray objectAtIndex:0]];
这现在包含在iOS 5中,称为RightBarButtonims,注意复数形式 以下是来自以下网站的文本: 右巴氏体 要显示在导航栏右侧的自定义栏按钮项数组 当接收器是顶部导航项时 @属性(非原子,副本)NSArray*RightBarButtonims 讨论 此数组可以包含0个或多个条形按钮项,以显示在列表的右侧
导航栏。项目从右到左的显示顺序与它们在列表中的显示顺序相同 数组。因此,数组中的第一个项是最右边的项,并添加其他项 在上一项的左侧 如果没有足够的空间显示数组中的所有项,则 重叠标题视图(如果存在)或栏左侧的按钮不存在
显示 还可以使用RightBarButtonim属性设置数组中的第一项 在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];
要显示独立按钮(而不是分段控制按钮),请执行以下操作:
您可以从Interface Builder执行此操作。我在我的应用程序中所做的-只是在导航栏的左侧添加了UIView,并在该视图中放置了3个按钮,并连接了它们的操作。您可以按照此操作在左/右导航项中添加多个按钮 参考:
如果有人路过,这里有一个快速的答案:
let barButton_array: [UIBarButtonItem] = [Button1, Button2]
navigationItem.setRightBarButtonItems(barButton_array, animated: false)
这对我帮助很大。谢谢!:)这确实很有帮助。只是不知道为什么它没有被接受。当我想在我自己的应用程序的右侧放置两个按钮时,这并不是我想要的,但是它的编码和运行比人们通过各种方式一起黑客来完成它要干净得多,所以我将取代我心目中的原始图像。这真的有效吗?我在没有luc[u viewTypeButton=[[UIButton alloc]initWithFrame:CGRectMake(0,0,20,20)]的情况下尝试过这个方法_viewTypeButton.titleLabel.text=@“R”;[self.navigationController.navigationBar添加子视图:_viewTypeButton];k、 在发行说明中完全忽略了这一点。谢谢是的,我也这么做了,直到有人向我透露了这件事+感谢您抽出时间更新这篇旧文章。我帮了很多忙。
let barButton_array: [UIBarButtonItem] = [Button1, Button2]
navigationItem.setRightBarButtonItems(barButton_array, animated: false)