Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add UIBarButtonItem to UIToolbar programmatically?

Here's what I need.

I have a free version and a paid version of my app. When the paid version loads, I need 3 UIBarButtons on my UIToolBar. When the Free version loads I need 4 UIBarButtons. On the far right barButton, I need the tint Blue, while the rest are default black. And I'm assuming the flexible space between them to even them out. I've tried doing this through IB, but I can't seem to get it to work with the spacing correct. As you can see, the bottom toolbar with 3 buttons are not spaced evenly with the toolbar. (I would like to do this programmatically)

enter image description here

enter image description here

#ifdef LITE_VERSION


#else

[buyButton removeFromSuperview];

#endif
like image 460
Jason Avatar asked Jun 18 '26 22:06

Jason


1 Answers

UIToolbar* toolbar = [[UIToolbar alloc]
                  initWithFrame:CGRectMake(0, 0, 320, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];

// create an array for the buttons
NSMutableArray* BarbuttonsArray = [[NSMutableArray alloc] initWithCapacity:5];

// create a clearAll button
UIBarButtonItem * clearAllButton = [[UIBarButtonItem alloc] initWithTitle:@"Clear All" 
                             style:UIBarButtonItemStylePlain
                             target:self
                             action:@selector(clearAllAction:)];

clearAllButton.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray  addObject:clearAllButton];
[clearAllButton release];


 // create a calculate button
 UIBarButtonItem *calculateButton = [[UIBarButtonItem alloc]initWithTitle:@"Calculate" 
                             style:UIBarButtonItemStylePlain
                           target:self
                           action:@selector(calculateButton:)];
calculateButton.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray  addObject:calculateButton];
[calculateButton release];


// create a settingButton
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc]initWithTitle:@"Setting" 
                             style:UIBarButtonItemStylePlain
                             target:self
                             action:@selector(settingButton:)];
settingButton.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray  addObject:settingButton];
[settingButton release];


 // create a buyNowButton

UIBarButtonItem *buyNowButton = [[UIBarButtonItem alloc]initWithTitle:@"Buy Now" 
                             style:UIBarButtonItemStylePlain
                          target:self
                          action:@selector(buyNowButton:)];
buyNowButton.style = UIBarButtonItemStyleBordered;
[BarbuttonsArray  addObject:buyNowButton];
[buyNowButton release];


 // put the BarbuttonsArray in the toolbar and release them
[toolbar setItems:BarbuttonsArray  animated:NO];
[BarbuttonsArray  release];

// place the toolbar into the navigation bar
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[toolbar release];


//before using these lines of code you have to alloc and make the property of   UINavigationController in your appDelegate

Try to use this may help you

like image 87
Abhishek Avatar answered Jun 21 '26 12:06

Abhishek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!