How to add several UIBarButtonItems to a NavigationBar? - iphone

I want to draw multiple buttons on a UINavigationBar. These will be either on right side or left side.

I did one example in which I had two buttons ( i.e. Edit and +) on Right side of NaviagationBar.
1) You have to create one NSMutableArray(i.e. "buttons" in example) and add UIBarButtonItem (i.e. bi1 and bi2 in example) to the NSMutableArray (i.e. buttons).
2) Add NSMutableArray(i.e. buttons in example) to toolbar(i.e. UIToolbar *tools in example).
3) Add toolbar to NavigationBar.
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add bar button1.
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStylePlain target:self action:#selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
//[bi1 release]; Do not release if ARC enabled.
// Add bar button2.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:#"+" style:UIBarButtonItemStylePlain target:self action:#selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
//[bi2 release]; Do not release if ARC enabled.
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
//[buttons release]; Do not release if ARC enabled.
// Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
//[twoButtons release]; Do not release if ARC enabled.

do it in your xib file and make properties or just variables in the header
#property (nonatomic, retain) IBOutlet UIBarButtonItem *itemOne;
and then connect it in the xib. Enjoy

Create a new UIToolbar in code and add your buttons to the toolbar. Then set self.navigationItem.rightBarButton to your newly created toolbar (note the example is without ARC so you may need to remove calls to release):
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addRow)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
[buttons addObject:self.editButtonItem];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];

Here I'm giving you sample code that I used for Button as well as label. you can create button instead of label and image what i created. I hope it will help you
- (void) setLabelForPotraite {
bar = [self.navigationController navigationBar];
[bar setBackgroundColor:[UIColor clearColor]];
barImg=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"navImg.png"]];
[bar addSubview:barImg];
tick_img_lbl=[[UIImageView alloc]initWithFrame:CGRectMake(86, 6,34, 33)];
tick_img_lbl.image=[UIImage imageNamed:#"tick-1.png"];
[bar addSubview:tick_img_lbl];
[tick_img_lbl release];
tickCount_lbl=[[UILabel alloc] initWithFrame:CGRectMake(126, 2, 50, 40)];
tickCount_lbl.text=#"";
tickCount_lbl.font=[UIFont fontWithName:#"Arial" size:24.0];
[tickCount_lbl setTextAlignment:UITextAlignmentCenter];
tickCount_lbl.font = [UIFont boldSystemFontOfSize:24.0];
tickCount_lbl.textColor=[UIColor whiteColor];
tickCount_lbl.backgroundColor=[UIColor clearColor];
[bar addSubview:tickCount_lbl];
[tickCount_lbl release];
cross_img_lbl=[[UIImageView alloc]initWithFrame:CGRectMake(181, 6, 34, 33)];
cross_img_lbl.image=[UIImage imageNamed:#"x_green.png"];
[bar addSubview:cross_img_lbl];
[cross_img_lbl release];
crossCount_lbl=[[UILabel alloc] initWithFrame:CGRectMake(221, 2, 50, 40)];
crossCount_lbl.text=#"";
crossCount_lbl.font=[UIFont fontWithName:#"Arial" size:24.0];
crossCount_lbl.font = [UIFont boldSystemFontOfSize:24.0];
crossCount_lbl.textColor=[UIColor whiteColor];
[crossCount_lbl setTextAlignment:UITextAlignmentCenter];
crossCount_lbl.backgroundColor=[UIColor clearColor];
[bar addSubview:crossCount_lbl];
[crossCount_lbl release];
master_img_lbl=[[UIImageView alloc]initWithFrame:CGRectMake(269, 6, 34, 33)];
master_img_lbl.image=[UIImage imageNamed:#"thumb.png"];
[bar addSubview:master_img_lbl];
[master_img_lbl release];
}
If any problem in understanding or else then plz inform me.

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.

Displaying multiple buttons in a navigation bar

In my application, I want to display 2 buttons (each having different action) in place of rightbarbutton of navigation bar. How can I do it?
here is a code that you want its working and helps
put Below code in
-(void)viewWillAppear:(BOOL)animated
{
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 103.0f, 44.01f)];
tools.tintColor = [UIColor clearColor];
[tools setBarStyle:UIBarStyleBlackTranslucent];
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
// Create a standard refresh button.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Settings.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(resetpass) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 30,30 )];
bi = [[UIBarButtonItem alloc] initWithCustomView:button];
[buttons addObject:bi];
[bi release];
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width = 2.0f;
[buttons addObject:bi];
[bi release];
// Add profile button.
bi = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(createnewFolders)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];
twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.leftBarButtonItem = twoButtons;
[twoButtons release];
}
thank you
I believe this post answers your question.
However, more information is required to answer your question properly. iOS version and so forth. That answer seems to have what you are requesting though.

How to create space between buttons?

I am already creating multiple buttons but I don't know how to align the buttons.
My code is here:
- (void)viewDidLoad
{
//self.title=#"Asset Management";
[super viewDidLoad];
listOfItems = [[NSMutableArray alloc] init];
[listOfItems addObject:#"User Information"];
[listOfItems addObject:#"Regional Settings"];
[listOfItems addObject:#"Configuration"];
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
//UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGs
toolbar.tintColor = [UIColor clearColor];
[toolbar setTranslucent:YES];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// Create button1
UIBarButtonItem *propertiesButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(button1Pressed)];
[buttons addObject:propertiesButton];
[propertiesButton release];
// Create button2
UIBarButtonItem *commentaryButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(button2Pressed)];
[buttons addObject:commentaryButton];
[commentaryButton release];
// Create button3
UIBarButtonItem *versionsButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(button3Pressed)];
[buttons addObject:versionsButton];
[versionsButton release];
// stick the buttons in the toolbar
[toolbar setItems:buttons animated:NO];
//self.toolbarItems = buttons;
[buttons release];
// and put the toolbar in the nav bar
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease]];
[toolbar release];
}
How do I create space b/w the buttons. Pls help me.
Thanks in advance!
You can add spaces between tool bar items using either of the two built-in space button types
UIBarButtonSystemItemFixedSpace and UIBarButtonSystemItemFlexibleSpace.
Fixed Space button
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[fixedSpace setWidth:20];
Flexible Space button
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
Add the space bar buttons in between the other tool bar items.

How can I set Custom Button where I want on UINavigationBar?

I have a View With UINavigationBar
Navigation Bar only permits me the button at fix position
With Fix Position at the sides of the navigation bar..
I want to customize the position of button...
Any Idea...will help me
Thanks in Advance
You can't customize the position of the buttons on a UINavigationItem, you only can set the rightBarButtonItem and leftBarButtonItem.
If you really need this, consider using a toolbar instead.
If you need a back button just like the navigation bar's back button, create a custom button and use an image.
Here's a PSD that will help.
You can position custom buttons within the UINavigationItem. This is how I added three buttons to the right:
// create a toolbar to have three buttons in the right (thanks Mart!)
tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 157, 44.01)];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 1.0, 157.0, 44.1)];
[imgView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"NavigationBarBackground" ofType:#"png"]]];
[tools addSubview:imgView];
[tools sendSubviewToBack:imgView];
[tools setTintColor:[UIColor colorWithRed:127/255.0 green:184/255.0 blue:72/255.0 alpha:1.0]];
[imgView release];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithTitle:#"Filter" style:UIBarButtonItemStyleBordered target:self action:#selector(showFilter:)];
[buttons addObject:bi];
[bi release];
bi = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"Map.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(showMap:)];
[buttons addObject:bi];
[bi release];
bi = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"Favourite.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(saveSearch:)];
[buttons addObject:bi];
[bi release];
[tools setItems:buttons animated:NO];
[buttons release];
rightBarButton = nil;
rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = rightBarButton;

Multiple UIBarButtonItems in UINavigationBar

How to Create multiple bar button in navigation bar?
From iOS 5 onwards, you can now do it using setLeftBarButtonItems:animated: or setRightBarButtonItems:animated:
You must use UIToolbar and set the toolbar with buttons:
// create a toolbar where we can place some buttons
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];
// create a standard save button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:#selector(saveAction:)];
saveButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:saveButton];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
// create a standard delete button with the trash icon
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:toolbar];
you have to create a view with as much button you required and have to add them on navigation button like following :
UIView *parentView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
UIButton *infoButton1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, 30, 32)];
[infoButton1 setBackgroundImage:[UIImage imageNamed: #"navbtn.png"] forState:UIControlStateNormal];
[infoButton1 setTitle:#"Back" forState:UIControlStateNormal];
infoButton1.titleLabel.font = [UIFont systemFontOfSize:13.0f];
infoButton1.titleLabel.textColor = [UIColor whiteColor];
[infoButton1 addTarget:self action:#selector(backBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[parentView1 addSubview:infoButton1];
[infoButton1 release];
UIButton *infoButton2 = [[UIButton alloc] initWithFrame:CGRectMake(30, 6, 30, 32)];
[infoButton2 setBackgroundImage:[UIImage imageNamed: #"navbtn.png"] forState:UIControlStateNormal];
[infoButton2 setTitle:#"Back" forState:UIControlStateNormal];
infoButton2.titleLabel.font = [UIFont systemFontOfSize:13.0f];
infoButton2.titleLabel.textColor = [UIColor whiteColor];
[infoButton2 addTarget:self action:#selector(backBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[parentView1 addSubview:infoButton2];
[infoButton2 release];
UIBarButtonItem *customBarButtomItem1 = [[UIBarButtonItem alloc] initWithCustomView:parentView1];
[parentView1 release];
self.navigationItem.leftBarButtonItem = customBarButtomItem1;
[customBarButtomItem1 release];`enter code here`
I know this question was already closed, but I find that the UIToolbar solution doesn't match visually.
If you instead use a second UINavigationBar set with a UINavigationItem that has a title of nil and the desired buttons you can add more buttons and have a bar that visually matches the original.
For iOS7 and higher, this is the right way to do it. No need for UIToolbar silliness.
- (void)viewDidLoad {
[super viewDidLoad];
[self configureView];
// create three funky nav bar buttons
UIBarButtonItem *one = [[UIBarButtonItem alloc]initWithTitle:#"One" style:UIBarButtonItemStylePlain target:self action:#selector(testMethod)];
UIBarButtonItem *two = [[UIBarButtonItem alloc]initWithTitle:#"Two" style:UIBarButtonItemStylePlain target:self action:#selector(testMethod)];
UIBarButtonItem *three = [[UIBarButtonItem alloc]initWithTitle:#"Three" style:UIBarButtonItemStylePlain target:self action:#selector(testMethod)];
// create a spacer
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
space.width = 30;
NSArray *buttons = #[one, space, two, space, three];
self.navigationItem.rightBarButtonItems = buttons;
}
I hate putting links as answers on SO as they can die anytime so i added relevant code taken from HERE
- (void)viewWillAppear
{
// get a view and :
[self.navigationController.navigationBar addSubView:yourView];
}