I want to add a bar right below NavigationController which will visible all the time even as user scrolls down. Can you tell me how I can do that or refer me to an article? I want to add couple of buttons to it to sort out results.
Thanks
If the youtube app most viewed section (http://www.engadget.com/photos/the-definitive-iphone-user-interface-gallery/#294309) is what you are after, then you don't need a bar below the navigation bar.
You could make a UISegmentedControl and add it as a custom title view on the navigation item of your view controller. Then if you want a title to also be displayed above the buttons you would set the prompt property on the navigation item.
So, in the init method of view controller that has the scrollable view:
self.navigationItem.prompt = #"Title of this view";
NSArray *items = [NSArray arrayWithObjects:#"Sort1", #"Sort2", nil];
UISegmentedControl *control = [[[UISegmentedControl alloc] initWithItems:items]
autorelease];
[control addTarget:self
action:#selector(action:)
forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = control;
As far as I know, it is impossible to nest multiple navigation controllers.
Related
I have problem in Navigation Controller. I can't create dynamic buttons on Navigation Bar.
I have used Navigation Bar. Inside this, I have one TabBar.
And Inside TabBar, I have one more Navigation Controller.
I can create buttons on that. But I want Dynamic buttons on parent Navigation controller of TabBar.
For Button It's not working.
My Code:
UIBarButtonItem *searchBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(btnClick:)];
//[self.navigationItem setRightBarButtonItem:searchBtn];
[app.navigationController.navigationItem setRightBarButtonItem:searchBtn];
[searchBtn release];
For Hide the parent Navigation Controller, It's working.
app.navigationController.navigationBarHidden =YES;
How can I solve this problem.?
if i understand you correctly you should change this
[app.navigationController.navigationItem setRightBarButtonItem:searchBtn];
[searchBtn release];
into
[app.navigationItem setRightBarButtonItem:searchBtn];
[searchBtn release];
I have a navigation toolbar in which I am adding toolbar items programatically, as below. The toolbar displays properly, and the toolbar style is set to black opaque. but the button on the toolbar does not display. Why?
//Set up the toolbar
[[[self navigationController] toolbar] setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *myButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(handleMyButton)];
NSArray *myItems = [NSArray arrayWithObjects: myButtonItem,nil];
[[self navigationController] setToolbarItems:myItems animated:NO];
[myButtonItem release];
UINavigationController fetches the buttons that should be used for the navigation bar and the tool bar from the current visible view controller. This means that you add the buttons you want to have to the view controller, not the navigation controller. So it should work just fine if you do:
[self setToolbarItems:myItems animated:NO];
Compare that with how the add button is added to the navigation bar in the default template for a Navigation Based Application with Core Data:
self.navigationItem.rightBarButtonItem = addButton;
This means that when you push a new view controller the buttons in the tool bar will disappear and then appear again when you pop back.
You are referencing the toolbar owned by your navigationController in the first line and not in the 4th line. It would appear that the necessary fix is:
[[[self navigationController] toolbar] setToolbarItems:myItems animated:NO];
instead of your current line 4.
Show the toolbar by setting the toolbarHidden property of the navigation controller object to NO.
To assign buttons to the toolbar you would call this method
[toolbar setItems:];
Instead of
[[self navigationController] setToolbarItems: animated:];
I am wondering why my UINavigationController is not letting me set a title. It lets me change the tint of the navigationBar, but the title and rightBarButtonItem I set just get ignored. Why?
Here's my code:
taps = 0;
UIView*controllerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
controllerView.backgroundColor = [UIColor whiteColor];
controller = [[UIViewController alloc] init];
[controller setView:controllerView];
[controllerView release];
navController = [[UINavigationController alloc] initWithRootViewController:controller];
navController.navigationBar.barStyle = 1;
navController.navigationItem.title = #"Setup";
UIBarButtonItem*item = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonSystemItemDone target:self action:#selector(dismissSetup:)];
navController.navigationItem.rightBarButtonItem = item;
[item release];
[self presentModalViewController:navController animated:YES];
[mp stop];
p.s: I know i'm not releasing some of the stuff I alloc'ed, I do that later!
set: self.title = #"This is my title"; in the viewController
(or in your case set controller.title = #"this is my title";)
The navigation controller updates the
middle of the navigation bar as
follows:
If the new top-level view controller has a custom title view,
the navigation bar displays that view
in place of the default title view.
To specify a custom title view, set
the titleView property of the view
controller’s navigation item.
If no custom title view is set, the navigation bar displays a
label containing the view
controller’s default title. The string
for this label is usually obtained
from the title property of the view
controller itself. If you want to
display a different title than the one
associated with the
view controller, set the title
property of the view controller’s
navigation item instead.
You want your UINavigationController to change its title depending on which viewcontroller is currently on top. So the way to go is to set the title in the viewcontrollers you are pushing onto the viewControllers array.
And by the way: I don't think navigationcontrollers are supposed to be presented modally:
Because the UINavigationController
class inherits from the
UIViewController class, navigation
controllers have their own view that
is accessible through the view
property. When deploying a navigation
interface, you must install this view
as the root of whatever view hierarchy
you are creating. For example, if you
are deploying the navigation interface
by itself, you would make this view
the main subview of your window. To
install a navigation interface inside
a tab bar interface, you would install
the navigation controller’s view as
the root view of the appropriate tab.
I'm trying to either add 3 custom buttons to my navigation controller toolbar on the top of my view or add a segmented control with 3 options. I have the following code on my app delegate for when i create my view controller(fwc) but the buttons dont appear.
/*
Set up the navigation controller for the Feeding Tab
*/
// instantiate the feedingViewController and set the title to Feedings
feedingViewController *fwc =
[[feedingViewController alloc] initWithNibName:#"feedingViewController"
bundle:[NSBundle mainBundle]];
//fwc.title = #"Feedings";
// set the tab bar item up and add it as feedingViewController's tab bar item
UITabBarItem *feedingTabBarItem =
[[UITabBarItem alloc] initWithTitle:#"Feedings" image:nil tag:0];
fwc.tabBarItem = feedingTabBarItem;
[feedingTabBarItem release];
// create a new nav controller for feedings and add root view
feedingNavController = [[UINavigationController alloc] init];
//Create the add button, need to change the selector to something though *****
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(newFeeding)];
//self.navigationItem.rightBarButtonItem = add;
UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
// Create and configure the segmented control
UISegmentedControl *sortToggle = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:#"Ascending",#"Descending", nil]];
sortToggle.segmentedControlStyle = UISegmentedControlStyleBar;
sortToggle.selectedSegmentIndex = 0;
[sortToggle addTarget:self action:#selector(toggleSorting:)forControlEvents:UIControlEventValueChanged];
// Create the bar button item for the segmented control
UIBarButtonItem *sortToggleButtonItem = [[UIBarButtonItem alloc]initWithCustomView:sortToggle];
[sortToggle release];
// Set our toolbar items
feedingNavController.toolbarItems = [NSArray arrayWithObjects:
flexibleSpaceButtonItem,
sortToggleButtonItem,
flexibleSpaceButtonItem,
add,
nil];
feedingNavController.navigationController.navigationBarHidden=NO;
[sortToggleButtonItem release];
[add release];
// Push the feedingViewController on the nav stack and release it.
[feedingNavController pushViewController:fwc animated:NO];
[fwc release];
In order to use a UITabBar you would need a UITabBarController, which is different than the UINavigationController. A UITabBar has a fundamentally different use than a UISegmentedControl. It appears that the functionality you're trying to implement is not appropriate for a UITabBar. In your question description you mention trying to add these buttons to the "navigation controller toolbar on the top." A UINavigationController has a UINavigationBar, which is the bar that runs across the top, and a UIToolbar, which is the bar that appears at the bottom. The UIToolbar, by default, is set to hidden, but you get a UIToolbar for free whenever you create a UINavigationController (see the UINavigationController reference in Xcode).
Apple's NavBar demo shows how to put a UISegmentedControl into the UINavigationBar. Instead of a title, use a custom titleView to display the segmented control:
fwc.navigationItem.titleView = sortToggle;
If you want to put your add UIBarButtonItem in the UINavigationBar as well, you can use:
fwc.navigationItem.rightBarButtonItem = add;
Note that you shouldn't actually go about trying to customize the UINavigationController's navigation bar on your own. The proper way to customize is to have an individual view controller access it's own navigationItem and set the titleView and rightBarButtonItem with the items you want.
If you wish to approach your problem using a UIToolBar instead, meaning that your items will appear on the bottom of the screen, you can do something like this:
// Assume UIBarButtonItem *add, UIBarButtonItem *sortToggleButtonItem,
// and UIBarButtonItem *flexibleSpaceButtonItem are allocated
[fwc setToolbarItems:[NSArray arrayWithObjects:
flexibleSpaceButtonItem,
sortToggleButtonItem,
flexibleSpaceButtonItem,
add,
nil]];
[feedingNavController setToolbarHidden:NO];
In my application I'm using a Navigation Controller to push one view that loads a Tab Bar Controller and a custom Navigation Bar as well. The problem is that the Tab Bar disappears below the bottom of the screen, and I don't know what's causing the problem.
If I load a simple Tab Bar in the next view, it positions itself correctly... but what I need is a Tab Bar Controller, and in that case the Tab Bar disappears below the bottom. I have tried changing the view and size properties of the Tab Bar, but that did not solve the problem.
I also realised that the images and text of the tabs don't show (I have set up the "favourites" and "contacts" images and text, and they are big enough and should be visible on the top side of the tab, but they are not).
Both tabs work perfectly, by the way.
There is an image here.
I load the Tab Bar with the following code:
- (void)viewDidLoad {
[super viewDidLoad];
myTabBarController = [[UITabBarController alloc] init];
SettingsViewController* tab1 = [[SettingsViewController alloc] init];
AboutViewController* tab2 = [[AboutViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:tab1, tab2, nil];
myTabBarController.viewControllers = controllers;
[self.view insertSubview:myTabBarController.view belowSubview:myNavigationBar];
}
It doesn't matter if I remove the Navigation Bar or not. I have tested using this instead:
[self.view addSubview:myTabBarController.view];
... forgetting about the Navigation Bar, but the Tab Bar still goes under the bottom.
I don't know if the problem is in one of my NIB files or in how I load the view (although I do this as I read in the Apple's SDK documentation). Any ideas?
Another question would be... do you know how could I change the title of my Navigation Bar when I select the second tab? I imagine I would have to do it in viewDidLoad in AboutViewController.m, would that be correct?
Thanks for you time!
Please ask one question per submission - it will allow us to better help you.
For your first problem: you need to add the tab bar controller to the navigation controller, not to that view. The heirarchy should be:
Navigation Controller
Tab Bar Controller
Your View(s)
(Though Apple's documentation suggests that the tab bar should be application-wide and should thus be the root view, not the navigation controller).
For your second question, the text on the "Back" button is determined by the title property of the previous view controller. To change it, do the following (source):
- ( void )viewDidLoad
{
[ super viewDidLoad ];
UIBarButtonItem *backButton =
[[ UIBarButtonItem alloc ] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil ];
self.navigationItem.backBarButtonItem = backButton;
[ backButton release ];
}
EDIT: It appears you want to set the title of the second view controller in your navigation stack. That's easy:
- ( void )viewDidLoad
{
[ super viewDidLoad ];
self.title = #"Title";
}