导航栏上超过 1 个 rightBarButtonItem [英] More than 1 rightBarButtonItem on navigation bar

查看:18
本文介绍了导航栏上超过 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

这里是 apple 的文本文档:

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(nonatomic, copy) 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天全站免登陆