导航栏上有1个以上的rightBarButtonItem [英] More than 1 rightBarButtonItem on navigation bar

查看:150
本文介绍了导航栏上有1个以上的rightBarButtonItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to have two rightBarButtonItems on navigation bar. One for Edit and the other for Add.

显然我无法使用Interface Builder。

Obviously I can't make it using Interface Builder.

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

Does anybody know how to do it programmatically? Thanks!

推荐答案

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

This is now included in iOS 5 and is called rightBarButtonItems, notice the plural

以下是来自苹果文档


rightBarButtonItems

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(非原子,复制) NSArray * rightBarButtonItems

@property(nonatomic, copy) NSArray *rightBarButtonItems

讨论

此数组可包含0个或更多条形按钮项目,以显示在右侧

导航栏。项目从右到左显示的顺序与
数组中显示的顺序相同。因此,数组中的第一项是最右边的项目,其他项目在前一项目的左侧添加

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.

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

The first item in the array can also be set using the rightBarButtonItem property.

在UINavigationBar.h中声明

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

这篇关于导航栏上有1个以上的rightBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆