iPhone自定义导航栏按钮

// create a toolbar where we can place some buttons
UIToolbar* toolbar = [[UIToolbar alloc]
                        initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];

// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

// create a standard save button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemSave
    target:self
    action:@selector(saveAction:)];
saveButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:saveButton];
[saveButton release];

// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
    target:nil
    action:nil];
[buttons addObject:spacer];
[spacer release];

// create a standard delete button with the trash icon
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
    initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
    target:self
    action:@selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
[deleteButton release];

// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];

// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
                                           initWithCustomView:toolbar] autorelease];
[toolbar release];



原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://afantihust.blog.51cto.com/2231549/533893
通常使用的Navigation Item中有backBarButtonItem/leftBarButtonItem/rightBarButtonItem三个按键,其中backBarButtonItem为只读,不能修改,其他两个都可以自由修改.不但可以防止button,还可以用自定义的view来填充,比方说想在导航栏右侧防止两个按钮,如图所示
 
 
代码如下:
 UIView *rightBarView = [[UIView alloc]initWithFrame:CGRectMake(618, 0, 150, 44)];
 
 UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 [editBtn setFrame:CGRectMake(10, 10, 50, 25)];
 [editBtn addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
 [editBtn setTitle:@"编辑" forState:UIControlStateNormal];
 [editBtn setTag:11];
 [rightBarView addSubview:editBtn];
 
 UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 [submitBtn setFrame:CGRectMake(80, 10, 70, 25)];
 [submitBtn addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
 [submitBtn setTitle:@"提交订单" forState:UIControlStateNormal];
 [submitBtn setTag:12];
 [rightBarView addSubview:submitBtn];
 
 UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:rightBarView];
 self.navigationItem.rightBarButtonItem = rightBtn; 
 

本文出自 “何必呢” 博客,请务必保留此出处http://afantihust.blog.51cto.com/2231549/533893

第二种方式

   NSMutableArray *buttons=[[NSMutableArrayalloc]init];    

    UIBarButtonItem *phonebutton=[[UIBarButtonItemalloc]initWithImage:[UIImageimageNamed:@"phone.png"]style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(phoneclick)];

    UIBarButtonItem *mapbutton=[[UIBarButtonItemalloc]initWithImage:[UIImageimageNamed:@"c_address.png"]style:UIButtonTypeCustom target:selfaction:@selector(mapclick)];

    [buttons addObject:phonebutton];

    [buttons addObject:mapbutton];

    self.navigationItem.rightBarButtonItems=buttons;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值