How to implement the Bar Button item on navigation bar,using that Bar Button item i want to navigate to another page,As i am new to iphone development,seek your help.
Thanks in advance
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:#"title" style:UIBarButtonItemStylePlain target:self action:#selector(btnAction)];
[self.navigationItem setRightBarButtonItem:rightBarButton];
[rightBarButton release];
try this :
UIButton *btnPrev =[[UIButton alloc] init];
[btnPrev setBackgroundImage:[UIImage imageNamed:#"btnPrevious.png"] forState:UIControlStateNormal];
btnPrev.frame = CGRectMake(100, 100, 50, 30);
[btnPrev addTarget:self action:#selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnBack =[[UIBarButtonItem alloc] initWithCustomView:btnPrev];
self.navigationItem.leftBarButtonItem = btnBack;
Related
I need to add a button with no title and background image and perform action when clicked on it .I need to add a button to navigation bar.
I have made the navigation bar like this :
-(void)viewDidLoad{
[super viewDidLoad];
[self.navigationBar setFrame:CGRectMake(0,0,320,50)];
self.navigationBar.tintColor=[UIColor whiteColor];
}
I can see the white navigation bar to it.How can I add a button to it with image and action ?
use this code
-(void)viewDidLoad{
[super viewDidLoad];
[self.navigationBar setFrame:CGRectMake(0,0,320,50)];
self.navigationBar.tintColor=[UIColor whiteColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"yourImageName"]
style:UIBarButtonItemStyleBordered
target:self action:#selector(yourButtonPressMethodName:)];
self.navigationItem.leftBarButtonItem = item;//use rightBarButtonItem if you want to add button on right dife
}
You can make UIBarButtonItem with custom view:-
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0.0, 0.0, 75.0, 40.0)];
[btn setImage:[UIImage imageNamed:#"anyImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(yourAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barbutton = [[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = barbutton;
you can use code below:
UIBarButtonItem *rightBar=[[UIBarButtonItem alloc]initWithImage:#"img.png" style:UIBarButtonItemStyleBordered target:self action:#selector(methodname:)];
My problem is following: i want to know the coordinates to an item of a toolbar so that i could pop some data right there. These barbuttonitems never reveal me there frame properties.
Is there a solution?
You can use if you want to give spacing.
UIBarButtonItem *fixedLeftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
// for adding Custom view
UIButton *webButton = [UIButton buttonWithType:UIButtonTypeCustom];
[webButton setFrame:CGRectMake(0, 0, 50, 40)];
[webButton setImage:[UIImage imageNamed:#\"webIcon.jpg\"] forState:UIControlStateNormal];
[webButton addTarget:self action:#selector(goToWebPage) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *webpageBtn = [[UIBarButtonItem alloc]initWithCustomView:webButton];
[webpageBtn setTarget:self];
[webpageBtn setAction:#selector(goToWebPage)];
[toolbar setItems:[NSArray arrayWithObjects: webpageBtn,fixedLeftSpace,webpageBtn, nil]];
Also see the link.
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 210, 44)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
NSMutableArray* buttons = [[NSMutableArray alloc] initWithObjects:bt1,space,bt2,space,bt3, nil];
[tools setItems:buttons animated:YES];
UIBarButtonItem *toolsBtn = [[UIBarButtonItem alloc]initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = toolsBtn;
If you are creating a toolbar from xib file then you need to add multiple "flexible space bar button item" before and after the buttons if you multiple bar buttons on toolbar until you get the desired spacing.
If you are creating UIToolbar dynamically then you need to add flexible buttons dynamically.
Here is my code. This is adding image but images are being appeared in navigation bar.
UIToolbar* toolbartop = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray* buttonstop = [[NSMutableArray alloc] initWithCapacity:5];
UIBarButtonItem *remainderButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(LoadOption:)];
[buttonstop addObject:remainderButton];
[remainderButton release];
UIBarButtonItem *faveButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(homeAction)];
[buttonstop addObject:faveButton];
[faveButton release];
// put the buttons in the toolbar and release them
[toolbartop setItems:buttonstop animated:NO];
[buttonstop release];
// place the toolbar into the navigation bar
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbartop];
But this code is showing image in navigation right bar.
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(homeAction)];
self.navigationItem.rightBarButtonItem = settingButton;
Why left navigation bar button is not showing images?
[toolbartop release];
You can add the image to your left BarButtonItem as follows:
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,105,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"image1.png"] forState:UIControlStateNormal];
[button1 addTarget:appDelegate action:#selector(Open_Link1) forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [[UIButton alloc] init];
button2.frame=CGRectMake(105,0,105,30);
[button2 setBackgroundImage:[UIImage imageNamed: #"image2.png"] forState:UIControlStateNormal];
[button2 addTarget:appDelegate action:#selector(Open_Link2) forControlEvents:UIControlEventTouchUpInside];
UIView *viewButtons = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 210, 30)];
[viewButtons addSubview:button1];
[viewButtons addSubview:button2];
[button1 release];
[button2 release];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:viewButtons];
[viewButtons release];
Here please make sure that the methods "Open_Link1" and "Open_Link2" must exists in the code as follows:
-(void)Open_Link1
{
// Write your logic
}
-(void)Open_Link2
{
// Write your logic
}
Let me know if you want more help.
The left bar button item is automatically used by the framework for showing a "Back" button. You can't put anything here, at least not when your navigation bar is managed by a UINavigationController. For this to work you have to handle the UINavigationBar yourself.
Edit:
in your leftBarButtonItem, try putting a UISegmentedControl instead of a UIToolbar for toolbartop.
You are adding a button to toolbar but you aren't adding your toolbar to any view. Try to set the button directly using
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonStop];
i was working with navigation bar button items.i was using the following code to do so
UIBarButtonItem *btnSave = [[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(save_Clicked:)];
self.navigationItem.rightBarButtonItem = btnSave;
[btnSave release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc]
initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(save_Clicked)];
self.navigationItem.leftBarButtonItem = btnCancel;
[btnCancel release];
my question is how to add another button just adjacent to the left bar button item.
thanks in advance
To do this you need to create a toolbar then keep adding UIButton to it, then set the toolbar as the leftBarButton
something like this:
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 250, 44)];
tools.tintColor = [UIColor clearColor];
[tools setTranslucent:YES];
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:9];
UIImage *myImage = [UIImage imageNamed:#"AL_HomeMod_Icon.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(clickViewHomeMod) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *bi = [[UIBarButtonItem alloc]
initWithCustomView:myButton];
[buttons addObject:bi];
[bi release];
myImage = [UIImage imageNamed:#"AL_History_Icon.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(clickViewHistory) forControlEvents:UIControlEventTouchUpInside];
bi = [[UIBarButtonItem alloc]
initWithCustomView:myButton];
[buttons addObject:bi];
[bi release];
myImage = [UIImage imageNamed:#"AL_RX_Icon.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(clickViewCustomPopView2) forControlEvents:UIControlEventTouchUpInside];
bi = [[UIBarButtonItem alloc]
initWithCustomView:myButton];
[buttons addObject:bi];
[bi release];
myImage = [UIImage imageNamed:#"AL_User_Icon.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(clickViewCustomPopView:) forControlEvents:UIControlEventTouchUpInside];
bi = [[UIBarButtonItem alloc]
initWithCustomView:myButton];
[buttons addObject:bi];
popButton = myButton;
[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];
hope that help
Pondd
Create a button as
UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"logout.png"]
style:UIBarButtonItemStylePlain
target:self action:#selector(doLogout)];
Add this button to right of navigation bar
self.navigationItem.rightBarButtonItem = logoutButton;
or add this button to left side of navigation bar
self.navigationItem.leftBarButtonItem = logoutButton;
doLogout is a function which will be called on touch logout button
I achieved my task by using the following code :
UIToolbar *tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 150, 44)];
tools.backgroundColor=[UIColor clearColor];
[tools setTranslucent:YES];
UIBarButtonItem *optionBtn=[[UIBarButtonItem alloc]initWithTitle:#"Options" style:UIBarButtonItemStyleBordered target:self action:#selector(optionPressed:)];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(donePressed:)];
NSArray *buttons=[NSArray arrayWithObjects:optionBtn,doneBtn, nil];
[tools setItems:buttons animated:NO];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:tools];
NOTE:From IOS 5.0 onwards, apple has done it lot easier . It can be done as
self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:optionBtn,doneBtn, nil];
Create a custom view with two buttons and use UIBarButtonItem's initWithCustomView: initializer. That should do it.
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];
}