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


1、显示Toolbar

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

  1. [self.navigationControllersetToolbarHidden:NOanimated:YES];

iOS学习之UINavigationController详解与使用(三)ToolBar

2、在ToolBar上添加UIBarButtonItem

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

  1. UIBarButtonItem*one=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:nilaction:nil];
  2. UIBarButtonItem*two=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarkstarget:nilaction:nil];
  3. UIBarButtonItem*three=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemActiontarget:nilaction:nil];
  4. UIBarButtonItem*four=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdittarget:nilaction:nil];
  5. UIBarButtonItem*flexItem=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:nilaction:nil];
  6. [selfsetToolbarItems:[NSArrayarrayWithObjects:flexItem,one,flexItem,two,flexItem,three,flexItem,four,flexItem,nil]];

效果:

iOS学习之UINavigationController详解与使用(三)ToolBar


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


3、动态添加Toolbar

我们在SecondView添加动态的Toolbar。

在SecondViewController.h添加

  1. #import<UIKit/UIKit.h>
  2. @interfaceSecondViewController:UIViewController
  3. {
  4. UIToolbar*toolBar;
  5. }
  6. @end

在SecondViewController.m添加

  1. -(void)viewDidLoad
  2. {
  3. [superviewDidLoad];
  4. [self.navigationControllersetToolbarHidden:YESanimated:YES];
  5. UIBarButtonItem*addButton=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearchtarget:selfaction:@selector(gotoThridView:)];
  6. toolBar=[[UIToolbaralloc]initWithFrame:CGRectMake(0.0,self.view.frame.size.height-toolBar.frame.size.height-44.0,self.view.frame.size.width,44.0)];
  7. [toolBarsetBarStyle:UIBarStyleDefault];
  8. toolBar.autoresizingMask=UIViewAutoresizingFlexibleTopMargin;
  9. [toolBarsetItems:[NSArrayarrayWithObject:addButton]];
  10. [self.viewaddSubview:toolBar];
  11. //Doanyadditionalsetupafterloadingtheviewfromitsnib.
  12. }
先把RootView时显示的Toobar隐藏

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

[toolBarsetItems:[NSArrayarrayWithObject:addButton]];

BarButtonItem用 的是UIBarButtonSystemItemSearch, 效果如下:

iOS学习之UINavigationController详解与使用(三)ToolBar

4、新建ThridView,从SecondView跳转到

Commad+N新建一个ThridViewController,

这个addButton跳转到ThridView

  1. -(void)gotoThridView:(id)sender
  2. {
  3. ThridViewController*thridView=[[ThridViewControlleralloc]init];
  4. [self.navigationControllerpushViewController:thridViewanimated:YES];
  5. [email protected]"ThridView";
  6. }

跳转Second到Third效果:

iOS学习之UINavigationController详解与使用(三)ToolBar


到此UINavigationController练习的差不多了。

前面两篇:

iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

iOS学习之UINavigationController详解与使用()页面切换和segmentedController


例子代码:https://github.com/schelling/YcDemo

著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢

相关文章:

  • 2021-12-16
  • 2021-08-27
  • 2021-11-11
  • 2022-12-23
  • 2021-12-19
  • 2021-11-26
猜你喜欢
  • 2022-12-23
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案