NavigationController导航栏中添加多个UIBarButtonItem

在实际的开发中,导航器是最重要的容器之一,我们经常要在导航栏中添加各种样式的按钮,添加一个按钮很简单,代码如下图:

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Setting" style:UITabBarSystemItemContacts 
                                                                 target:self action:@selector(clickSettings:)];          
self.navigationItem.rightBarButtonItem = anotherButton; 
[anotherButton release];

其中按钮的样式可以有多种,具体的可以参考:https://developer.apple.com/library/ios/prerelease/#documentation/UIKit/Reference/UIBarButtonItem_Class/

在有些项目中要在右面添加两个按钮,实现的样式如下图:

image

 

实现的代码如下图:

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 45)];
[tools setTintColor:[self.navigationController.navigationBar tintColor]]; 
[tools setAlpha:[self.navigationController.navigationBar alpha]]; 
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                        target:self action:@selector(clickSettings:)];

UIBarButtonItem *anotherButton1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UITabBarSystemItemContacts 
                                                        target:self action:@selector(clickEdit:)]; 
[buttons addObject:anotherButton]; 
[anotherButton release]; 
[buttons addObject:anotherButton1]; 
[anotherButton1 release]; 
[tools setItems:buttons animated:NO]; 
[buttons release]; 
UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
self.navigationItem.rightBarButtonItem = myBtn;

[myBtn release]; 
[tools release];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值