在UIToolBar上添加按钮,并调整按钮位置

{app.params.name}} {app.params.name}} {app.params.name}}

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

如下代码

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];
这里面用的是横屏的,我针对于自己的项目,需要的是竖屏的,结果的代码如下:

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


版权声明

davidzhang 创作并维护的 Gowhich 博客采用 创作共用保留署名-非商业-禁止演绎4.0国际许可证。

本文首发于 Gowhich 博客( https://www.gowhich.com ),版权所有,侵权必究。

本文永久链接: https://www.gowhich.com/blog/174

comments powered by Disqus