IOS 定义UIToolbar里的UIBarButtonItem两种方式,从Button创建和从Title创建

    UIToolbar *mycustomToolBar;
    NSMutableArray *mycustomButtons = [[NSMutableArray alloc] init];
    
    ///第一个按钮,后退
    UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setBackgroundImage:[UIImage imageNamed:@"btn_top_menu_back_normal.png"] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(GotoSettings) forControlEvents:UIControlEventTouchUpInside];
    button1.frame=CGRectMake(0,0,20,20);

    UIBarButtonItem *myButton1 = [[UIBarButtonItem alloc]initWithCustomView:button1];
    
    [mycustomButtons addObject: myButton1];
    //第二个按钮
    UIBarButtonItem *myButton2 = [[UIBarButtonItem alloc]
                                  initWithTitle:@"童锁"
                                  style:UIBarButtonItemStylePlain
                                  target:self
                                  action:@selector(action)];
    myButton2.width = 40;
    [myButton2 setImage:[UIImage imageNamed:@"btn_top_menu_back_normal.png"] ];
    [mycustomButtons addObject: myButton2];
    
    mycustomToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 44.0f)];
    mycustomToolBar.barStyle = UIBarStyleDefault;
    [mycustomToolBar setItems:mycustomButtons animated:YES];
    [mycustomToolBar sizeToFit];
    self.navigationItem.titleView = mycustomToolBar;

你可能感兴趣的:(ios,UIButton,控件,uiimage)