Jason
Jason

Reputation: 650

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

Upvotes: 2

Views: 6048

Answers (4)

ganesh manoj
ganesh manoj

Reputation: 977

IBOutlet UITabBarItem * item1;

like this create 4 items... give link to your xib so that we can hide required tabbarItem when ever you want... I hope this will help

Upvotes: 0

Dan F
Dan F

Reputation: 17732

Add the buy button to your toolbar by default, and tag it with some unique value (say -1), and then at runtime you can remove it from the toolbar like so:

#ifdef LITE_VERSION
    //Dont need to do anything, view is set up as lite version by default
#else

    NSArray *elements = toolBar.items;
    NSMutableArray *newElements = [[NSMutableArray alloc] initWithCapacity:[elements count]];

   for ( UIBarItem *item in elements )
   {
        if ( item.tag != -1 )
        {
            //Item is not the buy button, add it back to the array
            [newElements addObject:item];
        }
    }

    [toolBar setItems:newElements];

#endif

This is some code I use in one of my apps already to replace an item that is tagged in IB with -1 with a specific button at runtime, depending on which view is on display

Upvotes: 0

Abhishek
Abhishek

Reputation: 2253

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

Upvotes: 10

Mani
Mani

Reputation: 1841

First add buy button on your toolbar. bind and synthesize.

if(AppPaid) { 
    NSMutableArray *items = [YourToolBar.items mutableCopy];
    [items removeObject:yourBuyButton];
    YourToolBar.items = items;
    [items release];
  }

Upvotes: 0

Related Questions