How to add bar buttons in a UIToolBar

后端 未结 2 425
清歌不尽
清歌不尽 2021-01-22 20:47

I have created a UIToolBar and want to add three items in that like contact, date and message. I tried but i am not able to do that.Kindly help as i am new to

2条回答
  •  庸人自扰
    2021-01-22 21:09

    In Following code i added two UIBarButton with flexSpace..
    you can add UIBarButton as you want

    UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        Toolbar.barStyle = UIBarStyleBlackTranslucent;
        [Toolbar sizeToFit];
    
         NSMutableArray *barItems = [[NSMutableArray alloc] init];
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        [barItems addObject:flexSpace];
        [flexSpace release];
    
        UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(Cancel)];
        [barItems addObject:btnCancel];
    
        UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(done)];
        [barItems addObject:btnDone];
    
        [Toolbar setItems:barItems animated:YES];
    

    Following Method is call when tapped on bar Button

    -(void)Cancel
    {
      // Write Code for Cancel Method
    }
    
    -(void)done
    {
      // Write Code for Done Method
    }
    

提交回复
热议问题