UINavigationController for iOS (3) ToolBar

Source: Internet
Author: User

IOSLearningUINavigationControllerDetails and usage(II)Page switching andSegmentedControllerNext, let's talk about the Navigation Toolbar.

 

1. Display Toolbar

Add the code in the-(void) viewDidLoad method of RootViewController. m so that Toobar is displayed.

 

    [self.navigationController  setToolbarHidden:NO animated:YES];

 

2. Add UIBarButtonItem on the ToolBar

Create several uibarbuttonitems and add them to the Toolbar as arrays.

 

UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];    UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];    UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];    UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];    UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];    [self setToolbarItems:[NSArray arrayWithObjects:flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem, nil]];

Effect:

 

 

Note: using [self. navigationController. toolbar setItems :( NSArray *) animated: <# (BOOL) #>] This method to add an item does not work. Now, when I add a Toolbar dynamically, this will take effect.

 

3. dynamically add Toolbar

We add a dynamic Toolbar in SecondView.

Add in SecondViewController. h

 

#import <UIKit/UIKit.h>@interface SecondViewController : UIViewController{    UIToolbar *toolBar;}@end

Add in SecondViewController. m

 

 

- (void)viewDidLoad{    [super viewDidLoad];    [self.navigationController  setToolbarHidden:YES animated:YES];    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(gotoThridView:)];    toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - toolBar.frame.size.height - 44.0, self.view.frame.size.width, 44.0)];    [toolBar setBarStyle:UIBarStyleDefault];    toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;    [toolBar setItems:[NSArray arrayWithObject:addButton]];    [self.view addSubview:toolBar];         // Do any additional setup after loading the view from its nib.}

First, hide the Toobar displayed during RootView.

 

[Self. navigationController setToolbarHidden: YESanimated: YES]; then, the new Toolbar is added to the SecondView and an Item is set for Toobar.

[ToolBarsetItems: [NSArrayarrayWithObject: addButton];

BarButtonItem uses UIBarButtonSystemItemSearch. The effect is as follows:

4. Create a New ThridView and jump from SecondView

Commad + N create a ThridViewController,

This addButton jumps to ThridView

 

-(void)gotoThridView:(id)sender{    ThridViewController *thridView = [[ThridViewController alloc] init];    [self.navigationController pushViewController:thridView animated:YES];    thridView.title = @"Thrid View";}

Jump Second to Third:

 

 

At this point, the UINavigationController has been practicing almost.

Previous two articles:

IOSLearningUINavigationControllerExplanation and use (1) AddUIBarButtonItem

IOSLearningUINavigationControllerDetails and usage(II)Page switching andSegmentedController

 

 

Example code: https://github.com/schelling/YcDemo

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.