Navigation Controllers
![](http://hi.csdn.net/attachment/201111/30/0_13226155240jg6.gif)
The Objects of a Navigation Interface
![](http://hi.csdn.net/attachment/201111/30/0_132261586604LZ.gif)
![](http://hi.csdn.net/attachment/201111/30/0_13226161123Gq3.gif)
Defining the Custom View Controllers for a Navigation Interface
Loading Your Navigation Interface from a Nib File
![](http://hi.csdn.net/attachment/201111/30/0_1322617120xbTB.gif)
Creating a Navigation Interface Programmatically
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UIViewController *rootController = [[MyRootViewController alloc] init];
navigationController = [[UINavigationController alloc]
initWithRootViewController:rootController];
[rootController release];
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
}
Adopting a Full-Screen Layout for Navigation Views
1. A view controller can ask that its view be displayed with a full-screen layout instead. In a full-screen layout, the content view is configured to underlap the navigation bar, status bar, and toolbar as appropriate.
2. When determining whether a view should be sized to fill all or most of the screen, a navigation controller considers several factors, including the following:
- Is the underlying window (or parent view) sized to fill the entire screen bounds?
- Is the navigation bar configured to be translucent?
- Is the navigation toolbar (if used) configured to be translucent?
- Is the underlying view controller’s wantsFullScreenLayout property set to YES?
- Configure the frame of your custom view to fill the screen bounds.
- Set the translucent property of your navigation controller to YES. This allows your content to underlap the navigation bar.
- To underlap the status bar, set the wantsFullScreenLayout property of your view controller to YES. (The navigation bar must be translucent in order for this attribute to be recognized.)
- To underlap an optional toolbar, set the translucent property of the toolbar to YES.
Modifying the Navigation Stack
![](http://hi.csdn.net/attachment/201111/30/0_132261856753om.gif)
Monitoring Changes to the Navigation Stack
![](http://hi.csdn.net/attachment/201111/30/0_1322618948684W.gif)
Customizing the Navigation Bar Appearance
Configuring the Navigation Item Object
![](http://hi.csdn.net/attachment/201111/30/0_1322619468bl5j.gif)
1. Although most of the navigation bar’s content is obtained from the topmost navigation item, a pointer to the back item is maintained so that a back button (with the title of the preceding item) can be created.
![](http://hi.csdn.net/attachment/201111/30/0_1322619883I4IK.gif)
![](http://hi.csdn.net/attachment/201111/30/0_13226201365qvg.gif)
Showing and Hiding the Navigation Bar
2. Because the user needs the back button on the navigation bar to navigate back to the previous screen, you should never hide the navigation bar without giving the user some way to get back to the previous screen. The most common way to provide navigation support is to intercept touch events and use them to toggle the visibility of the navigation bar.
Modifying the Navigation Bar Object Directly
- barStyle property
- translucent property
- tintColor property
![](http://hi.csdn.net/attachment/201111/30/0_1322620578lcWs.gif)
Using Custom Buttons and Views as Navigation Items
2. For the topmost view controller, the item that is displayed on the left side of the navigation bar is determined using the following rules:
- If you assign a custom bar button item to the leftBarButtonItem property of the topmost view controller’s navigation item, that item is given the highest preference.
- If you do not provide a custom bar button item and the navigation item of the view controller one level down on the navigation stack has a valid item in its backBarButtonItem property, the navigation bar displays that item.
- If a bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the navigation stack.
3. For the topmost view controller, the item that is displayed in the center of the navigation bar is determined using the following rules:
- If you assign a custom view to the titleView property of the topmost view controller’s navigation item, the navigation bar displays that view.
- If no custom title view is set, the navigation bar displays a custom view containing the view controller’s title.
4. For the topmost view controller, the item that is displayed on the right side of the navigation bar is determined using the following rules:
- If the new top-level view controller has a custom right bar button item, that item is displayed. To specify a custom right bar button item, set the rightBarButtonItem property of the navigation item.
- If no custom right bar button item is specified, the navigation bar displays nothing on the right side of the bar.
![](http://hi.csdn.net/attachment/201111/30/0_1322621374rsYT.gif)
// View 3 - Custom right bar button with a view
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:@"up.png"],
[UIImage imageNamed:@"down.png"],
nil]];
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
You should create the items in the viewDidLoad method of your view controller.
Using Edit and Done Buttons
1. The editButtonItem method of UIViewController returns a preconfigured button that when pressed toggles between an Edit and Done button and calls the view controller’s setEditing:animated: method with appropriate values. To add this button to your view controller’s navigation bar, you would use code similar to the following:myViewController.navigationItem.rightBarButtonItem = [myViewController editButtonItem];
2. If you include this button in your navigation bar, you must also override your view controller’s setEditing:animated: method and use it to adjust your view hierarchy.
Displaying a Navigation Toolbar
1. To configure a toolbar for your navigation interface, you must do the following:
- Show the toolbar by setting the toolbarHidden property of the navigation controller object to NO.
- Assign an array of UIBarButtonItem objects to the toolbarItems property of each of your custom view controllers
![](http://hi.csdn.net/attachment/201111/30/0_1322622053wnPz.gif)
Specifying the Toolbar Items
- (void)configureToolbarItems
{
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
self.toolbarItems = [NSArray arrayWithObjects:
flexibleSpaceButtonItem,
sortToggleButtonItem,
flexibleSpaceButtonItem,
nil];
[sortToggleButtonItem release];
[flexibleSpaceButtonItem release];
}
Showing and Hiding the Toolbar
1. To hide the toolbar for a specific view controller, set the hidesBottomBarWhenPushed property of that view controller to YES.
2. If you want to hide the toolbar sometimes (but not always), you can call the setToolbarHidden:animated: method of the navigation controller at any time.