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