How to add bar buttons in a UIToolBar

后端 未结 2 396
清歌不尽
清歌不尽 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
    }
    
    0 讨论(0)
  • 2021-01-22 21:10

    try this...

    UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
        myToolbar.barStyle = UIBarStyleBlackOpaque;
    
        UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
        UIBarButtonItem *contactBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(contact:)];
    
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
     UIBarButtonItem *dateBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(date:)];
    
    UIBarButtonItem *flexibleSpaceRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
     UIBarButtonItem *msgBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(message:)];
    
    [myToolbar setItems:[NSArray arrayWithObjects: flexibleSpaceLeft, doneBtn, flexibleSpace, dateBtn, flexibleSpaceRight, msgBtn, nil]];
    
    0 讨论(0)
提交回复
热议问题