我是第一次使用这个东西,经过自己资料的查找,终于有了我觉得比较好用的方法

如下代码

1
2
3
4
5
6
7
8
UIButton *nextStep = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 10.0, 70.0, 30.0)];
[nextStep setImage:[UIImage imageNamed:@"button-下一步.png"] forState:UIControlStateNormal];
[nextStep addTarget:self action:@selector(doneAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *nextStepBarBtn = [[UIBarButtonItem alloc] initWithCustomView:nextStep];
//[nextStepBarBtn setWidth:1080];
UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target:nil action:nil];
[spaceButtonItem setWidth:540];
[myToolBar setItems:[NSArray arrayWithObjects:spaceButtonItem,nextStepBarBtn,nil] animated:YES];

这里面用的是横屏的,我针对于自己的项目,需要的是竖屏的,结果的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
UIToolbar *attentionToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - 100.0, self.view.frame.size.width, 60.0)];
attentionToolBar.tintColor = [UIColor redColor];
attentionToolBar.backgroundColor = [UIColor redColor];
[self.view addSubview:attentionToolBar];


UIButton *attentionBar = [[UIButton alloc] initWithFrame:CGRectMake(200.0, 10.0, 80.0, 30.0)];
[attentionBar setTitle:@"添加关注" forState:UIControlStateNormal];
[attentionBar addTarget:self action:@selector(addAttention:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *nextStepBarBtn = [[UIBarButtonItem alloc] initWithCustomView:attentionBar];

UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target:nil action:nil];
[spaceButtonItem setWidth:(self.view.frame.size.width - attentionBar.frame.size.width)/2];

[attentionToolBar setItems:[NSArray arrayWithObjects:spaceButtonItem,nextStepBarBtn,nil] animated:YES];