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;
Related
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.
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.
this is a simple problem, but i'm new to xcode dev. I followed a guide online to have multiple buttons on a navigation bar. the edit button on the navgivation bar has a IBAction method called "editButton". which has a (id)sender as parameter. do I get the sender and change the text from edit to done, done to edit?
"UIBarButtonitem *bbi = (UIBarButonItem *) sender;" doesn't seem to be working. how do i get the button in the toolbar in navigationbar?
Thank you.
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 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:NULL];
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];
// create a standard "EDIT" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editButton:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// 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];
[tools release];
-(IBAction)editButton:(id) sender{
UIBarButtonitem *bbi = (UIBarButonItem *) sender;
if (bbi title isequalsString:#"Done){
[bbi setTitle:#"Edit"];
}
}else{
[bbi setTitle:#"Done"];
}
}
The trouble is that you have used a UIBarButtonSystemItemEdit, not a standard uibarbutton.
Try creating it with:
bi = [UIBarButtonItem alloc] initWithTitle:#"Edit" style: UIBarButtonItemStyleBordered target:self action:#selector(editButton:)];
Then use the rest of the code as is.
UIBarButtonSystemItemEdit is a special bar button item that takes care of the "Edit / Done" states for you. No need to manually change the button's text.
how can i add buttons just beside a left barButton or just beside rightbarButton,
i mean i need to have more than 2 buttons on the title bar is that possible?
thanx in advance
You can use a view with two buttons in your leftBarButton -
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftBtnsView];
As drawn from this blog post:
Think of a toolbar as your container,
instead of the UIView object. And
since a UIToolBar is UIView based, it
can be added to the right side of a
nav bar using the above trick. The
following code shows how you can add
two standard buttons to the right
side:
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 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:NULL];
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];
// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// 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];
[tools release];
Also check the SO post
Adding buttons to navigation bar
I would suggest to use UISegmentedControl to hold all your buttons and use
[[UIButton alloc] initWithCustomView:] to show the segment control.
Here is a sample of how you could go about doing this. I am using a MasterDetailView template that comes with the top navigation bar for this example:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] init];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
leftButton.title = #"Left button";
rightButton.title = #"right button";
self.navigationItem.leftBarButtonItem = leftButton;
self.navigationItem.rightBarButtonItem = rightButton;
}
You can do something like this, there's something called UINavigationItem.rightBarButtonItems
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"xxx.png"] style:UIBarButtonItemStyleDone target:self action:#selector(something)];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"xxx.png"]
style:UIBarButtonItemStyleDone target:self
action:#selector(something)];
self.navigationItem.rightBarButtonItems = #[settingsButton, menuButton];
i think you need something like this
UIBarButtonItem *firstButton = [[UIBarButtonItem alloc] initWithTitle:#"First" style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *secondButton = [[UIBarButtonItem alloc] initWithTitle:#"Second" style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationController.navigationItem.leftBarButtonItems=#[firstButton, secondButton];
it will add two bar button items on left of navigation bar. you can make it on right side the same way
self.navigationController.navigationItem.rightBarButtonItems=#[firstButton, secondButton];
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