IOS NavigationController Toolbar学习笔记

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

[self.navigationController setToolbarHidden:NO animated:YES]

     显示如下图:

 

2、在ToolBar上添加UIBarButtonItem

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

 UIBarButtonItem *camera=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(ClickToolBarButton)];
    [camera setWidth:80];
    UIBarButtonItem *refresh=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(ClickToolBarButton)];
    [refresh setWidth:80];
    UIBarButtonItem *reply=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(ClickToolBarButton)];
    [reply setWidth:80];
    UIBarButtonItem *compose=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(ClickToolBarButton)];
    [compose setWidth:80];
    
    UIBarButtonItem *splitspace=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    [self setToolbarItems:[NSArray arrayWithObjects:splitspace,camera,splitspace,refresh,splitspace,reply,splitspace,compose,splitspace, nil]];

定义点击事件,代码如下:
-(void)ClickToolBarButton{
    NSLog(@"你点击了!");
}
最终运行效果图如下:


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

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

 - (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setToolbarHidden:YES animated:YES];
    //自定义的UIView
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
    
    [btn addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *firstButton=[[UIBarButtonItem alloc] initWithCustomView:btn];
    [firstButton setWidth:120];
    //系统自带的view
    UIBarButtonItem *addButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:nil];
    toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-toolbar.frame.size.height-44, self.view.frame.size.width, 44)];
    [toolbar setBarStyle:UIBarStyleDefault];
    toolbar.autoresizingMask=UIViewAutoresizingFlexibleTopMargin;
    [toolbar setItems:[NSArray arrayWithObjects:addButton,firstButton,nil]];
    [self.view addSubview:toolbar];
    //Do any additional setup after loading the view from its nib.
}


运行以后界面如下:


工程文件下载地址:下载

  IOS NavigaionController讲解一地址:http://blog.csdn.net/u011872945/article/details/16944037

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是 iOS Navigation Controller 中常用的一些方法的详解: 1. pushViewController:animated: 方法:将一个新的视图控制器推入导航栈中,并在导航条上显示一个新的视图控制器的标题和返回按钮。可以通过 animated 参数来控制是否显示动画效果。 2. popViewControllerAnimated: 方法:从导航栈中弹出当前的视图控制器,并返回上一个视图控制器。可以通过 animated 参数来控制是否显示动画效果。 3. popToViewController:animated: 方法:从导航栈中弹出所有在指定视图控制器之上的视图控制器,并返回到指定的视图控制器。可以通过 animated 参数来控制是否显示动画效果。 4. popToRootViewControllerAnimated: 方法:从导航栈中弹出所有的视图控制器,并返回到根视图控制器。可以通过 animated 参数来控制是否显示动画效果。 5. setViewControllers:animated: 方法:设置导航栈中的所有视图控制器,并可以通过 animated 参数来控制是否显示动画效果。 6. topViewController 属性:获取当前导航栈中的顶部视图控制器。 7. visibleViewController 属性:获取当前导航栈中可见的视图控制器。 8. navigationBar 属性:获取导航条对象,可以通过该对象修改导航条的样式或添加自定义视图。 以上是 iOS Navigation Controller 中常用的一些方法和属性的详解,掌握这些方法可以更好地实现导航控制器的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值