Generally, the navigation controller contains four objects: uinavigationcontroller, uinavigationbar, uiviewcontroller, and uinavigationitem;
Navigationitem is stored on uinavigationbar. We can know that a navigation controller controls multiple views, and a view controller controls a uinavigationitem.
Initialize navigationcontroller
RootViewController *rootViewController = [[RootViewController alloc] init];// UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];// // [navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_background.png"] forBarMetrics:UIBarMetricsDefault]; NavigationViewController *navigationController = [[NavigationViewController alloc] initWithRootViewController:rootViewController];
Navigationviewcontroller inherits the uinavigationcontroller
Navigationbar
One navigation controller controls multiple views. The leftitem, rightitem, and title on the navigationbar are controlled by the current view controller. Let's take a look at the creation method:
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(study)]; self.navigationItem.leftBarButtonItem = leftItem; [leftItem release];
Let's take a look at self. navigationitem. leftbarbuttonitem indicates a hierarchical relationship. items are controlled by the navigationitem of the current view controller. If they are written as self. navigationcontroller. navigationitem. leftbarbuttonitem does not work, which is equivalent to adding leftitem to each view controller, which violates our original intention!
if([self.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){ [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar_background.png"] forBarMetrics:UIBarMetricsDefault]; }
This statement can only be used in Versions later than ios5, and a background image is set for navigationbar.
Toolbar
The usage of toolbar is similar to that of navigationbar, except that toolbar is managed by array. This is very important and hidden by default. Remember to set it to open if you want to use it.
Uibarbuttonitem * tbaritem = [[uibarbuttonitem alloc] lower: lower target: Self action: @ selector (book)]; uibarbuttonitem * tbaritem1 = [[uibarbuttonitem alloc] lower: lower target: self action: Nil]; uibarbuttonitem * tbaritem3 = [[uibarbuttonitem alloc] initwithtitle: @ "title" style :( uibarbuttonitemstyledone) Target: Self action: Nil]; uibarbuttonitem * tbaritem4 = [[uibarbuttonitem alloc] initwithtitle: @ "button1" style :( uibarbuttonitemstylebordered) Target: Self action: Nil]; // set the interval uibarbuttonitem * flex = [[uibarbuttonitem alloc] lower: lower target: Self action: Nil]; nsarray * itemarray = @ [tbaritem, flex, tbaritem1, flex, tbaritem3, flex, tbaritem4]; // The default value is hidden [self. navigationcontroller settoolbarhidden: No animated: Yes]; [self settoolbaritems: itemarray];
Be sure to open it, otherwise it will not be displayed!
Set the display of navigationbar and tabbar
[self.navigationController setToolbarHidden:NO animated:YES]; [self.navigationController setNavigationBarHidden:NO animated:YES];