UINavigationController详解与使用(三)ToolBar
UINavigationController详解与使用(二)页面切换和segmentedController 接上篇,我们接着讲Navigation 的Toolbar。
运行效果图:
第一个界面
[img]
[/img]
第二个界面
[img]
[/img]
第三个界面
[img]
[/img]
1、显示Toolbar
在RootViewController.m的- (void)viewDidLoad方法中添加代码,这样Toobar就显示出来了。
[self.navigationController setToolbarHidden:NO animated:YES];
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]];
#import <UIKit/UIKit.h>@interface SecondViewController : UIViewController{ UIToolbar *toolBar;}@end
- (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.}
-(void)gotoThridView:(id)sender{ ThridViewController *thridView = [[ThridViewController alloc] init]; [self.navigationController pushViewController:thridView animated:YES]; thridView.title = @"Thrid View";}
-(void)viewWillAppear:(BOOL)animated{[self.navigationController setToolbarHidden:NO animated:YES];}
//首先需要创建一个NSMutableArray NSMutableArray *buttons=[[NSMutableArray alloc]initWithCapacity:3];[buttons autorelease];//创建一个 UIBarButtonItem 系统刷新按钮 并且加入到Array中UIBarButtonItem *freshButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemRefresh target:self action:@selector(OnrefreshMap:)];[buttons addObject:freshButton];[freshButton release];//创建一个空格 ,加入到array,用来将下面加入的按钮按照右边对齐UIBarButtonItem *SpaceButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];[buttons addObject:SpaceButton];[SpaceButton release];//创建一个 系统 搜索按钮,加入到array,放到右边UIBarButtonItem *searchSelfButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemSearch target:self action:@selector(OnFindSelf:)];[buttons addObject:searchSelfButton];[searchSelfButton release];//最后,将array 设置给toolbar[toolbar setItems:buttons animated:YES];[toolbar sizeToFit];