iOS学习之UINavigationController详解与使用(三)ToolBar
来源:互联网 发布:漕宝路梦海桑拿js照片 编辑:程序博客网 时间:2024/05/16 10:36
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.
- }
[self.navigationController setToolbarHidden:YESanimated:YES];然后把新建的Toolbar添加的SecondView中,并为Toobar设置了一个Item.
[toolBarsetItems:[NSArrayarrayWithObject:addButton]];
BarButtonItem用 的是UIBarButtonSystemItemSearch, 效果如下:
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";
- }
跳转Second到Third效果:
到此UINavigationController练习的差不多了。
前面两篇:
iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
例子代码:https://github.com/schelling/YcDemo
- iOS学习之UINavigationController详解与使用(三)ToolBar
- IOS学习之UINavigationController详解与使用(三)ToolBar
- IOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar .
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- iOS学习之UINavigationController详解与使用(三)ToolBar
- 技术(4)—智能指针
- PLINQ 简介
- 读 Beginning Android Games 2nd Edition (十) 创建绘图类
- 第十次周总结--在路上
- matlab字符串处理
- iOS学习之UINavigationController详解与使用(三)ToolBar
- 将Linq转换为SQL的便捷方法
- 记录几个比较nb的技术性网站
- PowerDesigner代码关联名称映射设置
- Unity3D AnimationView动画视图、动画事件
- 一段获取当前用户Sid的小代码
- Unity3D ParticleSystem粒子系统(3.5版)【初级】
- Oracle10g for AIX 安装实施文档
- Ajax中的XMLHttpRequest对象详解