码迷,mamicode.com
首页 > 移动开发 > 详细

IOS NavigationController Toolbar学习笔记

时间:2014-10-30 10:48:21      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   使用   for   

1、首先将toolbar显示出来,在viewDidLoad中添加代码让toolbar显示,代码如下:

 

 

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

 

 

     显示如下图:

 bubuko.com,布布扣

2、在ToolBar上添加UIBarButtonItem

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

  1. UIBarButtonItem *camera=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(ClickToolBarButton)];  
  2.    [camera setWidth:80];  
  3.    UIBarButtonItem *refresh=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(ClickToolBarButton)];  
  4.    [refresh setWidth:80];  
  5.    UIBarButtonItem *reply=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(ClickToolBarButton)];  
  6.    [reply setWidth:80];  
  7.    UIBarButtonItem *compose=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(ClickToolBarButton)];  
  8.    [compose setWidth:80];  
  9.      
  10.    UIBarButtonItem *splitspace=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];  
  11.      
  12.    [self setToolbarItems:[NSArray arrayWithObjects:splitspace,camera,splitspace,refresh,splitspace,reply,splitspace,compose,splitspace, nil nil]];  


定义点击事件,代码如下:

  1. -(void)ClickToolBarButton{  
  2.     NSLog(@"你点击了!");  
  3. }  

最终运行效果图如下:

 

bubuko.com,布布扣

说明:使用[self.navigationController setToolbarItems:[NSArray arrayWithObjects:splitspace,camera,splitspace,refresh,splitspace,reply,splitspace,compose,splitspace, nil] animated:YES];添加是不起作用的。

3、自定义Toolbar,首先新建一个页面,在头文件中声明一下UIToolbar *toolbar;
   在实现文件中的viewDidLoad方法中实现自定义Toolbar,实现代码如下:
 

  1. - (void)viewDidLoad  
  2.   
  3.    [super viewDidLoad];  
  4.    [self.navigationController setToolbarHidden:YES animated:YES];  
  5.    //自定义的UIView  
  6.    UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];  
  7.      
  8.    [btn addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];  
  9.    UIBarButtonItem *firstButton=[[UIBarButtonItem alloc] initWithCustomView:btn];  
  10.    [firstButton setWidth:120];  
  11.    //系统自带的view  
  12.    UIBarButtonItem *addButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:nil];  
  13.    toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-toolbar.frame.size.height-44, self.view.frame.size.width, 44)];  
  14.    [toolbar setBarStyle:UIBarStyleDefault];  
  15.    toolbar.autoresizingMask=UIViewAutoresizingFlexibleTopMargin;  
  16.    [toolbar setItems:[NSArray arrayWithObjects:addButton,firstButton,nil]];  
  17.    [self.view addSubview:toolbar];  
  18.    //Do any additional setup after loading the view from its nib.  



运行以后界面如下:

 

bubuko.com,布布扣

IOS NavigationController Toolbar学习笔记

标签:style   blog   http   io   color   os   ar   使用   for   

原文地址:http://www.cnblogs.com/huangh/p/4061913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!