zoukankan      html  css  js  c++  java
  • IOS NavigationController Toolbar学习笔记

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

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

         显示如下图:

     

    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. }  

    最终运行效果图如下:

    说明:使用[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.  



    运行以后界面如下:

  • 相关阅读:
    网络编程基础之TCP编程学习(一)
    日常巡检(脚本)
    Mysql主主配置
    创建用户和相对应的随机密码
    企业网络连接测试
    Docker 方式部署 Gitlab 并使用自定义的http端口
    Vue学习笔记(二):使用Axios进行跨域请求
    Vue学习笔记(一):引入Bootstrap和jQuery
    C# / .NET Core 调用javascript方法(适用于Windows/Linux平台)
    Centos7.7安装bcm43142(博通)无线网卡驱动
  • 原文地址:https://www.cnblogs.com/huangh/p/4061913.html
Copyright © 2011-2022 走看看