UINavigationController详解与使用(三)ToolBar

本文详细介绍了如何在iOS应用中使用Navigation Controller的Toolbar,包括显示Toolbar、在Toolbar上添加UIBarButtonItem、动态添加Toolbar以及如何处理返回时Toolbar消失的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UINavigationController详解与使用(二)页面切换和segmentedController 接上篇,我们接着讲Navigation 的Toolbar。

运行效果图:

第一个界面
[img]
[img]http://dl.iteye.com/upload/attachment/0078/5821/58b4cf25-18d3-3caa-a8c6-13ee0cb968bc.jpg[/img]
[/img]

第二个界面
[img]
[img]http://dl.iteye.com/upload/attachment/0078/5823/66d3ddfd-5e52-3119-b6ef-ffb68e0c367c.jpg[/img]
[/img]

第三个界面
[img]
[img]http://dl.iteye.com/upload/attachment/0078/5825/690d87de-0cfb-3ed7-98f5-157d44447a62.jpg[/img]
[/img]

1、显示Toolbar
在RootViewController.m的- (void)viewDidLoad方法中添加代码,这样Toobar就显示出来了。
[self.navigationController  setToolbarHidden:NO animated:YES];


2、在ToolBar上添加UIBarButtonItem
新建几个UIBarButtonItem,然后以数组的形式添加到Toolbar中
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]];

注意:用 [self.navigationController.toolbar setItems:(NSArray *) animated:<#(BOOL)#>]这个方法添加item是不起效果的。下面我动态自己添加Toolbar时,这个才起效果。

3、动态添加Toolbar
我们在SecondView添加动态的Toolbar。
在SecondViewController.h添加
#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
{
UIToolbar *toolBar;
}
@end


在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.
}

先把RootView时显示的Toobar隐藏

[self.navigationController setToolbarHidden:YESanimated:YES];然后把新建的Toolbar添加的SecondView中,并为Toobar设置了一个Item.

[toolBarsetItems:[NSArrayarrayWithObject:addButton]];

4、新建ThridView,从SecondView跳转到

Commad+N新建一个ThridViewController,

这个addButton跳转到ThridView
-(void)gotoThridView:(id)sender
{
ThridViewController *thridView = [[ThridViewController alloc] init];
[self.navigationController pushViewController:thridView animated:YES];
thridView.title = @"Thrid View";

}


动态添加的时候,当从第二个View返回rootView的时候,rootView里面的toolBar消失了,该怎么办呢?
在RootViewController.m中添加该句,就可以了
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController setToolbarHidden:NO animated:YES];
}



如何给UIToolBar 添加 多个UIBarButtonItem . 在写一遍:
//首先需要创建一个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];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值