iPad app navigation bar with multiple buttons on the same side - objective-c

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/

Related

Title of Barbutton item in toolbar visible on IOS 6 but not on IOS 7

I am using the below code for toolbar & it is showing title for IOS 6 but not fro IOS 7.
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"multimedia/icon_settings.png"] style:UIBarButtonItemStylePlain target:self action:#selector(pressSettings:)];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"multimedia/icon_refresh.png"] style:UIBarButtonItemStylePlain target:self action:#selector(pressRefresh:)];
UIBarButtonItem *helpButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"multimedia/icon_help.png"] style:UIBarButtonItemStylePlain target:self action:#selector(pressHelp:)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
/* Set the title of the button */
[settingButton setTitle:#"Settings"];
[refreshButton setTitle:#"Refresh"];
[helpButton setTitle:#"Help"];
[feedbackButton setTitle:#"Feedback"];
NSArray *toolbarButtons = #[settingButton, flexSpace, helpButton, flexSpace, feedbackButton, flexSpace, refreshButton];
[self setToolbarItems:toolbarButtons];
This code working fine with IOS 6 but shows only bar button image in IOS 7 not title.
The UINavigationController maintains a UIToolBar for each view controller in its stack. This toolbar is normally hidden. So, you need to explicitly show the toolbar:
[self.navController setToolbarHidden:NO];
Get the navigation controller of your viewcontroller and set the above setToolbarHidden property to NO.
Hope it helps you.

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.

Set navigation bar title to UIBarButtonItem?

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;

adding menue or buttons for Top Tab bar in objective-C

I want to add 4 buttons or 4 menues in my TOP TAB BAR or top navigation bar, would you please help me that how can I do that should I do that via interface or programmatically and how?
Thanks in advance!
I'm really new to the iOS!
If using iOS 5, you can use the rightBarButtonItems/leftBarButtonItems property of the nav bar.
Just create an array of UIBarButtonItems, and assign that to the appropriate side.
UIBarButtonItem *button1= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(methodOne:)];
UIBarButtonItem *button2= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(methodTwo:)];
UIBarButtonItem *button3= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(methodthree:)];
UIBarButtonItem *button4= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(methodFour:)];
NSArray *buttons = [NSArray arrayWithObjects:button1,button2,button3,button4,nil];
Then, to put these on the left side of the navbar:
self.navigationItem.leftBarButtonItems = buttons;
Or to put them on the right side:
self.navigationItem.rightBarButtonItems = buttons;
You can also use the following to add spaces between your buttons:
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]
NSArray *buttons = [NSArray arrayWithObjects:button1,flexible,button2,flexible,button3,flexible button4,nil];
It's as simple as assigning UIBarButtonItems to your view controller's implied navigationController instance. Forgive the technical jargon, perhaps some code will make up for it:
//left
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]init];
//right
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]init];
Note that this won't work if your root view controller isn't a UINavigationController.

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;