分为4步骤
1、创建一个UIToolbar
2、创建一个NSMutableArray *buttons 用来存放UIBarButtonItem
3、buttons addObject 添加 按钮
4、[self.toolBar setItems:button];
5、self.view 添加toolbar
代码如下
action:NULL]; //不限制大小 自适应 self.toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 20, width, 0)]; //左侧取消按钮 UIBarButtonItem * leftBtn=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(back:)]; //右侧发布按钮 UIBarButtonItem * rightBtn=[[UIBarButtonItem alloc]initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(fabu:)]; NSMutableArray * buttons=[[NSMutableArray alloc]initWithCapacity:4]; [buttons addObject:leftBtn]; [buttons addObject:flexibleSpaceItem1]; [buttons addObject:rightBtn]; [self.toolBar setItems:buttons]; [self.toolBar sizeToFit]; self.toolBar.backgroundColor=color; self.toolBar.tintColor=[UIColor whiteColor]; [self.toolBar setBackgroundImage:[UIImage new]forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; [self.toolBar setShadowImage:[UIImage new] forToolbarPosition:UIToolbarPositionAny]; [self.view addSubview:self.toolBar];