如何将几个UIBarButtonItem添加到NavigationBar? [英] How to add several UIBarButtonItems to a NavigationBar?

查看:91
本文介绍了如何将几个UIBarButtonItem添加到NavigationBar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 UINavigationBar 上绘制多个按钮。这些将在右侧或左侧。

I want to draw multiple buttons on a UINavigationBar. These will be either on right side or left side.

推荐答案

我做了一个例子,其中我有两个按钮(即编辑和+)在NaviagationBar的右侧。

I did one example in which I had two buttons ( i.e. Edit and +) on Right side of NaviagationBar.

1)你必须创建一个 NSMutableArray (即按钮in示例)并将 UIBarButtonItem (例如bi1和bi2)添加到 NSMutableArray (即按钮)。

1) You have to create one NSMutableArray(i.e. "buttons" in example) and add UIBarButtonItem (i.e. bi1 and bi2 in example) to the NSMutableArray (i.e. buttons).

2)将 NSMutableArray (即示例中的按钮)添加到工具栏(即 UIToolbar * tools 例如)。

2) Add NSMutableArray(i.e. buttons in example) to toolbar(i.e. UIToolbar *tools in example).

3)向NavigationBar添加工具栏。

3) Add toolbar to NavigationBar.

 NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
 UIToolbar *tools = [[UIToolbar alloc]
                    initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add bar button1.

UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
//[bi1 release]; Do not release if ARC enabled.

// Add bar button2.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
//[bi2 release]; Do not release if ARC enabled.

// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
//[buttons release];  Do not release if ARC enabled.

 // Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
//[twoButtons release]; Do not release if ARC enabled.

这篇关于如何将几个UIBarButtonItem添加到NavigationBar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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