Add UIBarButtonItem to UIToolbar programmatically? - iphone

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)
#ifdef LITE_VERSION
#else
[buyButton removeFromSuperview];
#endif

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

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];
}

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

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

Related

Addimg mutiple button on Navigation bar in Iphone SDK

I want to add two buttons with custom image to Navigation Bar with some specific position.
I found solution But it is for Right/Left Navigation Bar Button.
My code for that is:
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add Pin button.
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStylePlain target:self action:#selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
[bi1 release];
// Add Hot Spot button.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:#"+" style:UIBarButtonItemStylePlain target:self action:#selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
[bi2 release];
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];
// Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
[twoButtons release];
How can i do this?
UIView *vieww =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[vieww addSubview:yourBtn1];
[vieww addSubview:yourBtn2];
[self.navigationController.navigationBar addSubview:vieww];
And if you want to remove yourButtonView then make is global object;
in .h
UIView *vieww;
and in .m
-(void)viewWillDisappear:(BOOL)animated
{
[vieww removeFromSuperview];
}
Or follow this for more Link
if you are using >iOS 5, then use this.
UIBarButtonItem *btn1=[[UIBarButtonItem alloc] initWithTitle:#" + " style:UIBarButtonItemStyleDone target:self action:#selector(action1:)];
UIBarButtonItem *btn2=[[UIBarButtonItem alloc] initWithTitle:#" - " style:UIBarButtonItemStyleDone target:self action:#selector(action2:) ];
self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:btn1,btn2,nil];
for < iOS 5 u can use following:
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 160, 44.01)];
tools.barStyle = UIBarStyleBlackOpaque;
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:4];
[buttons addObject:btn1];
[buttons addObject:btn2];
[tools setItems:buttons animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
Instead of adding toolbar, you can create one UIView, add two buttons on that view.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:yourView];
If you want to do this in combination with using a storyboard, take a look at this question.

Centering UIBarButtonItem in a UIToolbar and adding custom text

I need to add a button to the center of the ToolBar. I have done the adding the button to the toolbar part successfully. My problems are as follows;
1.) I need to center this barbutton. It should be in the center of the Tool Bar
2.) I need to have a text after the refresh button image is displayed.
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0 , 320 , 60)];
NSMutableArray* button = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshButtonAction:)];
[button addObject:barButton];
[toolBar setItems:button animated:NO];
[self.view addSubview:toolBar];
1. Add flexible spacers before and after your bar button in the toolbar items array:
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0 , 320 , 60)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshButtonAction:)];
NSArray *toolbarItems = [NSArray arrayWithObjects:flexibleSpace, barButton, flexibleSpace];
[toolBar setItems:toolbarItems animated:NO];
[self.view addSubview:toolBar];
Configuring toolbars is much easier to do in Interface Builder. If your view controller is inside a UINavigationController stack, you can still use IB to create an outlet collection of UIBarButtonItems and set self.toolbarItems in -viewDidLoad.
2. To get custom content in a toolbar you can create a bar button item with a custom view:
UIView *customView = <# anything, could be a UILabel #>;
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
I know this can be done in IB, but I believe if you want to center a button, you will need to add a fixed or flexible space button on either side to keep your button in the middle. If you are going to do this with just code... try and sandwich your button between the 2 space buttons.

Need 2 UIBarButtonItems where back button is located

I have a typical UINavigationController scheme but I have many many views which can mean you can end up having quite a few views stacked on top of each other. I want to provide a home button and originally I was going to put that on the right hand side of the navigation bar; however, I have a search bar there and another button so I am hoping to put it next to the back button.
In ascii art:
< Back | |Home| Title |Browse| [Search Bar]
Now I tried to set the backButtonItem of the previous view controller using the following:
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
back.title = #"Back";
// create a standard save button
[buttons addObject:back];
[back release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem *homeButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"home32.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(onHomeButton:)];
homeButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:homeButton];
[homeButton release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:toolbar];
[toolbar release];
but that doesn't seem to replace the standard back button. If I just try one UIBarButtonItem and set the backButton, that works.
Now the other approach I tried is that on the View that is pushed on the stack, to set the leftBarButtonItem but I can't seem to find a way to create a back button that has the pointy shape to the left.
Any suggestions?
can you try
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]
initWithCustomView:toolbar];
good luck

UIPickerView select and hide

How do you make a UIPickerView act like the one with a webview wherein there is a drop down selection box and instead of dropping down like usual websites do, the iphone makes it into a UIPickerView with all the selections in. When you select one, a check becomes visible beside your selection and changes the value of the drop box. And how do you put the "Done" button on top of the UIPickerView to dismiss the UIPickerView?
I already know that [pickerview setHidden:YES] is the method to use to hide the pickerview. I just don't know how to include the "Done" button in the UIPickerView.
Regards,
Chris
This piece of code will slide out a picker view as keyboard and attached a done button on top of it. Basically, you want to set a inputAccessoryView with your input field.
You should call this method on a touch down event for your input field.
- (IBAction)showYourPicker:(id)sender {
// create a UIPicker view as a custom keyboard view
UIPickerView* pickerView = [[UIPickerView alloc] init];
[pickerView sizeToFit];
pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
self.yourPickerView = pickerView; //UIPickerView
yourTextField.inputView = pickerView;
// create a done view + done button, attach to it a doneClicked action, and place it in a toolbar as an accessory input view...
// Prepare done button
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
keyboardDoneButtonView.tintColor = nil;
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self
action:#selector(pickerDoneClicked:)] autorelease];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
// Plug the keyboardDoneButtonView into the text field...
yourTextField.inputAccessoryView = keyboardDoneButtonView;
[pickerView release];
[keyboardDoneButtonView release];
}
Finally, your Done button calls the "pickerDoneClicked" method, where you should add
[yourTextField resignFirstResponder]; which will hide the picker view.
The "Done" button is placed in UIToolBar.
Use the below method of UIToolBar for adding the "Done" buttons.
- (void)setItems:(NSArray *)items animated:(BOOL)animated {
UIToolbar* mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
[mypickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(DatePickerDoneClick)];
[barItems addObject:doneBtn];
[mypickerToolbar setItems:barItems animated:YES];
}

How to hide one of the two Right buttons in the navigation bar

Dear all I have implemented two buttons in the navigation bar on the right side on top of a text view as;
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 112, 44.5)];
// toolbar style is the default style
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a button to run the job
UIBarButtonItem *runButton = [[UIBarButtonItem alloc]
initWithTitle:#"RUN"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(runAs:)];
// Button style is the default style
[buttons addObject:runButton];
[runButton release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
// create a standard Edit/Done button with custom titles Edit/Save
self.editButtonItem.possibleTitles = [NSSet setWithObjects:#"Edit", #"Save", nil];
self.editButtonItem.title = #"Edit";
UIBarButtonItem *editButton = self.editButtonItem;
[buttons addObject:editButton];
[editButton release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:YES];
[buttons release];
// place the toolbar into the navigation bar as Right Button item
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:toolbar];
[toolbar release];
Now in Edit mode I want to hide the RUN button and when the RUN button is in action I want the Edit button to be hidden. Can someone suggest me a way to do that without redefining the buttons in edit mode (like there is for the back/left button item setHidesBackButton:(BOOL) animated:(BOOL)) or any alternate method? Thanks a lot.
Try this:
UIToolbar *toolbar = (UIToolbar *) self.navigationItem.rightBarButtonItem.customView;
UIBarButtonItem *runButton = (UIBarButtonItem *) [toolbar.items objectAtIndex: 0];
runButton.customView.hidden = YES;
I needed to show/hide one of two buttons exclusively. I created a category on NSMutableArray, removed both buttons if they're in the array, then added each if necessary. You'll probably need to have the buttons as properties on your ViewController.
NSMutableArray+Extensions
#implementation NSMutableArray (Extensions)
-(bool)removeItemIfExists:(id)item {
bool wasRemoved=false;
for (int i=self.count-1;i>0;i--) {
if([self objectAtIndex:i] == item){
[self removeObjectAtIndex:i];
wasRemoved = true;
}
}
return wasRemoved;
}
#end
Call it using:
NSMutableArray *newLeftItems = [self.navigationItem.leftBarButtonItems mutableCopy];
[newLeftItems removeItemIfExists:btnOne];
[newLeftItems removeItemIfExists:btnTwo];
if(someCondition) {
[newLeftItems insertObject:btnOne atIndex:1];
} else {
[newLeftItems insertObject:btnTwo atIndex:1];
}
[self.navigationItem setLeftBarButtonItems:newLeftItems animated:true];
Try this..
-(void) changeBarButtonVisibility:(UIBarButtonItem*) barButtonItem visibility:(BOOL) shouldShow {
UIColor *tintColor = shouldShow == NO ? [UIColor clearColor] : nil;
[barButtonItem setEnabled:shouldShow];
[barButtonItem setTintColor:tintColor];
}
and call the above method and pass the bar button you want to hide
[self changeBarButtonVisibility:self.navigationItem.rightBarButtonItems[0] visibility:NO];
[self changeBarButtonVisibility:self.navigationItem.rightBarButtonItems[1] visibility:YES];