直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// TWFXToolBarViewController.m
// DemoToolBar
//
// Created by Lion User on 13-1-19.
// Copyright (c) 2013年 Lion User. All rights reserved.
//

#import "TWFXToolBarViewController.h"

@interface TWFXToolBarViewController ()

@end

@implementation TWFXToolBarViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization

//创建toolbar
UIToolbar *toolBar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 420.0f, 320.0f, 40.0f) ] autorelease];

//创建barbuttonitem
UIBarButtonItem *item1 = [[[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStyleBordered target:self action:@selector(test:)] autorelease];

//创建barbuttonitem
UIBarButtonItem *item2 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:nil] autorelease];

//创建一个segmentController
UISegmentedControl *seg = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"牛扒", @"排骨", nil] ] autorelease];

//设置style
[seg setSegmentedControlStyle:UISegmentedControlSegmentCenter];


[seg addTarget:self action:@selector(segmentControllerItem:) forControlEvents:UIControlEventValueChanged];

//创建一个内容是view的uibarbuttonitem
UIBarButtonItem *itemSeg = [[[UIBarButtonItem alloc] initWithCustomView:seg] autorelease];

//创建barbuttonitem,样式是flexible,这个种barbuttonitem用于两个barbuttonitem之间
//调整两个item之间的距离.flexible表示距离是动态的,fixed表示是固定的
UIBarButtonItem *flexible = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease];

//把item添加到toolbar里
[toolBar setItems:[NSArray arrayWithObjects:item1,flexible,itemSeg,flexible,item2, nil] animated:YES];

//把toolbar添加到view上
[self.view addSubview:toolBar];

}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

-(void)test:(id)sender
{
UIBarButtonItem *item = (UIBarButtonItem *) sender;
NSString *title = [NSString stringWithFormat:@"%@ 被选中了",item.title];

UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Attention" message:title delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil] autorelease];

[alertView show];
}


-(void)segmentControllerItem:(id)sender
{
UISegmentedControl *seg = (UISegmentedControl *) sender;
NSInteger index = seg.selectedSegmentIndex;
NSString *message;
if (index == 0) {
message = @"你选了牛扒";
}
else
{
message = @"你选了排骨";
}

UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Attenton" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] autorelease];

[alertView show];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)goBack:(UIButton *)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end

自己实践测试过了。效果很不错,在我之前做的和现在比的话,关键一点是“[toolBar setItems:[NSArray arrayWithObjects:item1,flexible,itemSeg,flexible,item2, nil] animated:YES];”这里很重要,使得自己不用去通过CGRectMake来设置。

也许你有个问题是我想使用UIButton,好的你跟我的想法是一样的,我贴一帖我的代码好了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-(void) showEditBar{
//创建toolbar
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat toolbarWidth = 200.0;
CGFloat toolbarHeight = 45.0;
CGFloat toolbarMarginBottom = 10.0;
CGFloat toolbarX = (screenRect.size.width - toolbarWidth) / 2;
CGFloat toolbarY = (screenRect.size.height - toolbarMarginBottom - toolbarHeight) - 2 * 44.0 - 22.0;

_editToolBar = [[DeleteToolBar alloc] initWithFrame:CGRectMake(toolbarX, toolbarY, toolbarWidth, toolbarHeight)];
_editToolBar.barStyle = UIBarStyleDefault;
_editToolBar.translucent = YES;

UIButton *deleteBar = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, toolbarWidth/2, _editToolBar.frame.size.height)];
deleteBar.titleLabel.font = [UIFont systemFontOfSize:24.0];
deleteBar.layer.backgroundColor = [[ColorConfig NavigationColor] CGColor];
deleteBar.layer.borderWidth = 2.0;
deleteBar.layer.borderColor = [[ColorConfig NavigationColor] CGColor];
deleteBar.layer.cornerRadius = 10.0;
deleteBar.alpha = 1.0;

[deleteBar setTitle:@"删除" forState:UIControlStateNormal];
[deleteBar addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *deleteBarBtn = [[UIBarButtonItem alloc] initWithCustomView:deleteBar];

//创建barbuttonitem,样式是flexible,这个种barbuttonitem用于两个barbuttonitem之间
//调整两个item之间的距离.flexible表示距离是动态的,fixed表示是固定的
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

//把item添加到toolbar里
[_editToolBar setItems:[NSArray arrayWithObjects:flexible,deleteBarBtn,flexible, nil] animated:YES];

//把toolbar添加到view上
[self.view addSubview:_editToolBar];

//设置table的位置
[self.dataTable setFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, TABLE_VIEW_HEIGHT - COMPARE_BAR_HEIGHT - 8.0)];
}

参考文章:http://www.cnblogs.com/zouzf/archive/2013/01/19/2867574.html