Navigation Bar That stays over the TabBarController - objective-c

I want to have a navigation bar with a settings button on it that stays there regardless of which tab it is on.
Sorry for the short message its late for me,
Chase

What you can do is drag a UIToolBar onto your storyboard controllers and set the buttons and the actions you want there. You could make all your controllers extend form the same base class as to not have to repeat your code, as well as instantiate this toolbar in code if you find it more pleasing.
The problem will be when it comes to using navigation as the navbar will replace/overlap with this toolbar.
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(myAction:)];
[myToolbar setItems:[NSArray arrayWithObjects:myButton, nil]];
[self.view addSubview:myToolbar];

In AppDidFinishLaunching:
Add Your TabBarcontroller (rootViewController) for UINavigationController
Now add setting button below is example in navigationBar
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonSystemItemDone target:nil action:nil];
navigationCOntroller.navigationItem.rightBarButtonItem = rightButton
rightButton will be visible throught application

If you just want to place a navigation bar with a settings button that can be accessed from any view using a UITabBarController you can do something like this in the AppDelegate -->
YourViewController1 *yourVC1 = [YourViewController1 alloc] initWithNibName:#"YourViewController1" bundle:nil];
UINavigationController *yourNVC1 = [[UINavigationController alloc] initWithRootViewController:yourVC1];
YourViewController2 *yourVC2 = [YourViewController2 alloc] initWithNibName:#"YourViewController2" bundle:nil];
UINavigationController *yourNVC2 = [[UINavigationController alloc] initWithRootViewController:yourVC2];
UITabBarController *tabBC = [[UITabBarController alloc] init];
[tabBC setViewControllers:[NSArray arrayWithObjects:yourNVC1, yourNVC2, nil]];
self.window.rootViewController = tabBC;
and you should have a tab bar with navigation bar on all the views. Now to place the settings button in the navigation bar, add this to the viewDidLoad method in your view controller where you want to show the settings button -->
UIBarButtonItem *selectBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered target:self action:#selector(selectorName:)];
self.navigationItem.rightBarButtonItem = selectBarButton;

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.

Why do I have to hide back button item before implementing custom left barButtonItem?

Here's my code for removing the back UIBarButtonItem of a navigation bar and replacing it with a cancel button:
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = cancelButtonItem;
Every example I've seen online doesn't hide the backButton before replacing it with a custom item. I may be wrong but it just seems like one unneeded line of code.
Did you try using UIBarButtonSystemItemCancel instead of UIBarButtonItemStylePlain in the style of your UIBarButtonItem?
Try this,
UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelAction)];
self.navigationItem.leftBarButtonItem = cancelButton;
Also if you have used self.navigationItem.backBarButtonItem in your parentViewController, it will show that button in the childViewController's leftBarButtonItem by default, if your custom leftBarButton in the childViewController is not added properly.
Try this.
Write this in your app delegates did finish launching.
LoginViewController *loginVc = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:loginVc];
self.window.rootViewController = self.navigationController;
self.navigationController.navigationBarHidden =YES;
// and use uibutton in your view controller at place of back button or cancel button
// and u can push or pop your view from there.

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.

Using a navigation bar to get something similar to the picker in the iPhone Safari app?

In Safari, when I press a dropdown box, a picker comes up with a navigation bar on top of it that has a done button and some other buttons.
I want a similar bar with two buttons above my picker view, but I can't figure out how to put buttons on the bar. With a navigation controller, I'd do:
self.navigationItem.rightBarButtonItem = [UIButton .....
self.navigationItem.backBarButtonItem = [UIButton ......
But I'm not finding any button properties on the navigation bar that I'm creating programmatically. Adding the buttons as subviews also doesn't seem to be doing anything. How can I add them?
It’s a toolbar, not a navigation bar. Set its items property to populate it with UIBarButtonItem objects. Note that those are not the same as UIButtons, and in fact don’t even subclass from UIView; they have their own set of attributes. A simple example:
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.items = [NSArray arrayWithObjects:
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed)] autorelease],
nil];
someTextField.inputAccessoryView = toolbar;
[toolbar release];

Buttons on top of UIPopoverController

I want to add two Buttons on top of UIPopoverController like it is shown in following screenshots:
HTML Edit
Thanks for helping me!
Add your view controller to a UINavigationController, then add the Navigation Controller to the UIPopoverController. Then in your UIViewController's viewDidLoad method, put this code in:
UIBarButtonItem *okButton = [[UIBarButtonItem alloc] initWithTitle:#"Ok" style:UIBarButtonItemStyleBordered target:self action:#selector(okayButtonPressed)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelButtonPressed)];
self.navigationItem.title = #"My Title";
[self.navigationItem setLeftBarButtonItem:cancelButton animated:NO];
[self.navigationItem setRightBarButtonItem:okButton animated:NO];
[cancelButton release];
[okButton release];
You need to initialize your popover with a UINavigationController directly. Then set the root view to your custom view controller.
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
Use a UINavigationController as the pop-over. Then, access the .navigationBar property of the navigation controller, get the .topItem, and set its .leftBarButtonItem and .rightBarButtonItem.
I wouldnt use a navigationcontroller like the previous posters suggested, apple recommends not using navigationcontrollers on ipad (with good reason) it doesnt behave as youd expect when pushing VC into the stack when used in popovers, now you dont really want to use the "navigation" aspect of it, but i wouldnt use navigationcontroller just because uw ant the bar....Use a UIToolBar instead, and set its buttons to whatever you want...no need to use a navigation controller here...
When I do this my navBar doesn't seem to fit properly inside of the UIPopoverController, as shown in the below:
http://www.flickr.com/photos/coleorton/4752223066/
Here's what I'm doing:
// alloc the Direct Reports view controller.
ToolsViewController *toolsViewController = [[[ToolsViewController alloc] init] autorelease];
UINavigationController *toolsNavController = [[[UINavigationController alloc] initWithRootViewController:toolsViewController] autorelease];
toolsNavController.title = #"Tools";
toolsNavController.view.frame = CGRectMake(0.0, -10.0, 320.0, POPOVER_HEIGHT);
if(![self.toolsPopoverController isPopoverVisible]){
// show popover
self.toolsPopoverController = [[[UIPopoverController alloc] initWithContentViewController:toolsNavController] autorelease];
self.toolsPopoverController.delegate = self;
self.toolsPopoverController.popoverContentSize = CGSizeMake(320.0, POPOVER_HEIGHT);
[self.toolsPopoverController presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
} else {
// close popover
[self.toolsPopoverController dismissPopoverAnimated:YES];
}
This worked!
//Determine how to present this view based on device
if ([UIDevice currentDevice ].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
BNRAssetTypeViewController *contentViewController = [[BNRAssetTypeViewController alloc] init];
UINavigationController *popOverNavigation = [[UINavigationController alloc] initWithRootViewController:contentViewController];
self.assetPickerPopover = [[UIPopoverController alloc] initWithContentViewController:popOverNavigation];
[self.assetPickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
then in the init function of contentViewController add this
//add a barbutton item which will help in adding new type
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addNew:)];
//set bar item to right side of navbarite
self.navigationItem.rightBarButtonItem =bbi ;