Set navigation bar title to UIBarButtonItem? - objective-c

here's some code that I am using to add UIBarButtonItems to my navigation bar, how can I change it display custom images, or images uploaded to my Xcode project?
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
NSArray *actionButtonItems = #[shareItem, cameraItem];
self.navigationItem.rightBarButtonItems = actionButtonItems;

UIBarButtonItem *shareItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"your_image.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"your_image.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects: shareItem, cameraItem, nil];
Also you can set not only your image, but also your custom UIView with this method:
- (id)initWithCustomView:(UIView *)customView
See more info.

UIBarButtonItemStyleBordered is deprecated in iOS 8. Use UIBarButtonItemStylePlain instead.
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"ic_clear"] style:UIBarButtonItemStylePlain target:self action:nil];
NSArray *actionButtonItems = #[shareItem];
self.navigationItem.leftBarButtonItems = actionButtonItems;

Related

How to recognize a button press for programmatic UIButton Xcode

Im using this code to create UIBarButtons programmatically on my Navigation Bar, how can I set the Buttons to send IBActions programmatically. I.O.W. how can I programmatically place a view to recognize the UITapGestureRecognizer(set the touchUpInside event) and connect it to an IBAction?
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:153/256.0 green:204/256.0 blue:51/256.0 alpha:1.0];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
self.navigationItem.title = #"Feed";
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
UIBarButtonItem *postItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"myIcon.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *addPlusItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
UIBarButtonItem *optionsItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"myIcon2.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
NSArray *cameraAndPostItem = #[cameraItem, postItem];
NSArray *addPlusAndOptionsItem = #[optionsItem, addPlusItem];
self.navigationItem.leftBarButtonItems = cameraAndPostItem;
self.navigationItem.rightBarButtonItems = addPlusAndOptionsItem;
EDIT: I am trying to perform a segue when the user presses the UIBarButtonItem, which I have implemented programmatically.
set the target and the action of the buttons
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
cameraItem.target = self;
cameraItem.action = #selector(cameraItemButtonInvoked);
//or short
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(cameraItemButtonInvoked)];
...
- (IBAction)cameraItemButtonInvoked {
.... // e.g.
[self performSegueWithIdentifier:#"ShowDetail" sender:sender];
}
You'll be setting the action to UIBarButton like this:
UIBarButtonItem *addPlusItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addSomething:)];
Note the action part, there you are passing the selector to be called. Write that selector in your ViewController like this:
- (void) addSomething:(id)sender{
NSLog(#"I'm adding something..");
}
Do the same for rest of the buttons.

changing the Navigation Bar controller text

I am trying to change the navigation bar controller text (by default this is the title of the previous controller)
in other words in need to change the "back" text in this image .
I tried several possibilities but none of them capture the new text :
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"New Title" style:UIBarButtonItemStyleBordered target:nil action:nil];
OR
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"New Title" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
OR
[[bar backItem] setTitle:#" New Title "];
OR
self.navigationItem.backBarButtonItem.title=#"New Title";
OR
self.navigationController.navigationBar.backItem.title=#"New Title";
am I missing something ?
I don't have the context to check, but try referring to it as the leftBarButtonItem.
Like this:
[self.navigationItem.leftBarButtonItem setText:#"SomethingSomethingDarkside"]
Try this:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"New Title" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.leftBarButtonItem = backButton;

How can I add multiple UIBarButtonItem's to a UINavigationBar?

I want to add many UIBarButtonItem's to a UINavigationbar, not just right and left buttons:
logoButton = [[UIBarButtonItem alloc] initWithTitle:#"A Button" style:UIBarButtonItemStyleBordered target:self action:#selector(logoButtonAClicked:)];
logoButton2 = [[UIBarButtonItem alloc] initWithTitle:#"B Button" style:UIBarButtonItemStyleBordered target:self action:#selector(logoButtonBClicked:)];
logoButto3 = [[UIBarButtonItem alloc] initWithTitle:#"C Button" style:UIBarButtonItemStyleBordered target:self action:#selector(logoButtonCClicked:)];
self.navigationController.navigationBarHidden = NO;
self.title = #"Title";
NSArray* items = [[NSArray alloc] initWithObjects:logoButtonA, logoButtonB, logoButtonC, nil];
self.navigationController.navigationBar.items = items;
I get a SIGBRT on self.navigationController.navigationBar.items = items;
How can I add multiple UIBarButtonItems to a UINavigationBar?
You need to add UIBarButtonItem instance to a UINavigationItem, not to a UINavigationBar. So you can do this as:
NSArray *buttonArray = [NSArray arrayWithObjects:logoButton, logoButton2, logoButton3, nil];
self.navigationItem.leftBarButtonItems = buttonArray;
If you want your buttons on the right, use rightBarButtonItems.
You should use
self.navigationItem.leftBarButtonItems = items;
use addSubView in navigationbar.

I am not able to add UIBarButtonItem in UINavigationController

navigator=[[UINavigationController alloc]initWithRootViewController:contacts];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]
initWithTitle:#"Delete"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(makeCall)];
[[self.navigator navigationItem] setLeftBarButtonItem:nextButton];
[nextButton release];
I am not able add UIBarButtonItem in UINavigationController ,please help
Speaking off the cuff here, perhaps instead of
[[self.navigator navigationItem] setLeftBarButtonItem:nextButton];
use
self.navigator.navigationBar.items = [NSArray arrayWithObject:nextButton];
to do what you want.

iPad app navigation bar with multiple buttons on the same side

how can I have a back button and another button both on the left side of the nav bar? since this is an ipad app there's space for multiple buttons. but I can only get it to show one button per side on the navigationitem...
For people who stumbled here and were disappointed by the broken link, check out this article:
http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html
iOS now supports multiple buttons on the same side. From the UINavigationItem docs:
Customizing Views
titleView property
leftBarButtonItems property
leftBarButtonItem property
rightBarButtonItems property
rightBarButtonItem property
– setLeftBarButtonItems:animated:
– setLeftBarButtonItem:animated:
– setRightBarButtonItems:animated:
– setRightBarButtonItem:animated:
So, basically, you would write something like this to add two buttons on the left:
UIBarButtonItem *refreshBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshPlans:)];
UIBarButtonItem *selectYearBtn = [[UIBarButtonItem alloc] initWithTitle:#"Select Year" style:UIBarButtonSystemItemAction target:self action:#selector(selectYear)];
self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWithObjects: refreshBtn, selectYearBtn, nil];
Unfortunately, I haven't seen a way to do this in the Storyboard. Hope this helps.
UIBarButtonItem *btn0 = [[UIBarButtonItem alloc] initWithTitle:#"1" style:UIBarButtonItemStylePlain target:self action:#selector(tapLevel0)];
UIBarButtonItem *btn1 =[[UIBarButtonItem alloc] initWithTitle:#"<2" style:UIBarButtonItemStylePlain target:self action:#selector(tapLevel1:)];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithTitle:#"<3" style:UIBarButtonItemStylePlain target:self action:#selector(tapLevel2:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space.width = 10;
UIBarButtonItem *space2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space2.width = 10;
// UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshItem)];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:btn0, space, btn1, space2, btn2, nil];
found the answer: http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/