分爲4步驟flex
一、建立一個UIToolbarcode
二、建立一個NSMutableArray *buttons 用來存放UIBarButtonItem ci
三、buttons addObject 添加 按鈕get
四、[self.toolBar setItems:button];it
五、self.view 添加toolbario
代碼以下table
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];