IOS NavigationController Toolbar学习笔记

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

 

 

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. [self.navigationController setToolbarHidden:NO animated:YES]  

 

 

     显示如下图:

 

2、在ToolBar上添加UIBarButtonItem

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

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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]];  


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

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. -(void)ClickToolBarButton{  
  2.     NSLog(@"你点击了!");  
  3. }  

最终运行效果图如下:

 

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

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

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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.  



运行以后界面如下:

 

转载于:https://www.cnblogs.com/huangh/p/4061913.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值