2

I want to add button instead of bar button item into toolbar programmatically. I select it because button has image infodark defaut.

so how is this done programmatically?

Cœur
  • 37,241
  • 25
  • 195
  • 267
kEvin
  • 411
  • 6
  • 18
  • you add a view on toolbar and then add a button on that toolbar thats work.first try with direct add button on toolbar if not then try with view – vishiphone May 04 '12 at 07:09

2 Answers2

7
UIImage* image = [UIImage imageNamed:defaultImage];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton* button = [[UIButton alloc] initWithFrame:frame];
[button setTitle:@"Display" forState:UIControlStateNormal & UIControlStateHighlighted];
[button setImage:image forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
[button addTarget:self action:@selector(button_Clicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
  • 1
    to add bar buttton item to toolbar self.forwardButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:nil action:nil]; NSArray *toolBarItems = [[NSArray alloc] initWithObjects:forwardButton,nil]; // Set toolar items [self.toolBar setItems:toolBarItems]; // add items to toolbar [self addSubview:self.toolBar]; EDIT In case you want to add Space between barbuttonitems UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL]; – kEvin May 04 '12 at 13:23
0

To add this button in UIToolbar you need this code. first you can initialize UIToolbar after that

NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:4];

[buttons addObject: barButtonItem];
[buttons addObject: btnFav];
[buttons addObject: btnGeneralPopOverController];
[buttons addObject: showInfo];   

[mytoolbar setItems:buttons animated:NO];
pableiros
  • 14,932
  • 12
  • 99
  • 105
Kevin
  • 15
  • 2