博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS学习之UINavigationController详解与使用(三)ToolBar
阅读量:4108 次
发布时间:2019-05-25

本文共 2637 字,大约阅读时间需要 8 分钟。

 接上篇,我们接着讲Navigation 的Toolbar。

1、显示Toolbar 

在RootViewController.m的- (void)viewDidLoad方法中添加代码,这样Toobar就显示出来了。

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


2、在ToolBar上添加UIBarButtonItem

新建几个UIBarButtonItem,然后以数组的形式添加到Toolbar中

[cpp] 
  1. UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];  
  2.     UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];  
  3.     UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];  
  4.     UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];  
  5.     UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];  
  6.     [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添加

[cpp] 
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface SecondViewController : UIViewController  
  4. {  
  5.     UIToolbar *toolBar;  
  6. }  
  7. @end  

在SecondViewController.m添加

[cpp] 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.   
  5.     [self.navigationController  setToolbarHidden:YES animated:YES];  
  6.   
  7.     UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(gotoThridView:)];  
  8.     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)];  
  9.     [toolBar setBarStyle:UIBarStyleDefault];  
  10.     toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;  
  11.     [toolBar setItems:[NSArray arrayWithObject:addButton]];  
  12.     [self.view addSubview:toolBar];  
  13.        
  14.     // Do any additional setup after loading the view from its nib.  
  15. }  
先把RootView时显示的Toobar隐藏

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

    [toolBarsetItems:[NSArrayarrayWithObject:addButton]]; 

BarButtonItem用 的是UIBarButtonSystemItemSearch, 效果如下:

4、新建ThridView,从SecondView跳转到

Commad+N新建一个ThridViewController,

这个addButton跳转到ThridView

[cpp] 
  1. -(void)gotoThridView:(id)sender  
  2. {  
  3.     ThridViewController *thridView = [[ThridViewController alloc] init];  
  4.     [self.navigationController pushViewController:thridView animated:YES];  
  5.     thridView.title = @"Thrid View";  
  6.   
  7. }  

跳转Second到Third效果:

到此UINavigationController练习的差不多了。

转载地址:http://ksvsi.baihongyu.com/

你可能感兴趣的文章
python __future__
查看>>
MySQL Tricks1
查看>>
python 变量作用域问题(经典坑)
查看>>
pytorch
查看>>
pytorch(二)
查看>>
pytorch(三)
查看>>
pytorch(四)
查看>>
pytorch(5)
查看>>
pytorch(6)
查看>>
opencv 指定版本下载
查看>>
ubuntu相关
查看>>
C++ 调用json
查看>>
nano中设置脚本开机自启动
查看>>
动态库调动态库
查看>>
Kubernetes集群搭建之CNI-Flanneld部署篇
查看>>
k8s web终端连接工具
查看>>
手绘VS码绘(一):静态图绘制(码绘使用P5.js)
查看>>
手绘VS码绘(二):动态图绘制(码绘使用Processing)
查看>>
基于P5.js的“绘画系统”
查看>>
《达芬奇的人生密码》观后感
查看>>