iOS: 工具栏控件UIToolBar和工具栏按钮控件UIBarButtonItem的使用

简介:

一、工具栏控件:UIToolBar:UIView

介绍:
ToolBar工具栏是视图View的属性,可以在工具栏上添加工具栏按钮Bar Button Item(可以是自定义的Custom、也可以是系统自带的BarButtonSystemItem ),视图控制器可以通过工具栏项对视图中内容进行操作。
 
注意事项:
在导航栏控制器中会有一个UIToolBar实例,但默认是隐藏的,如果需要显示,需要通过这个方法将其打开:

在这里需要注意的是,与UINavigationBar类似,导航控制器拥有且只拥有一个UIToolBar实例,但UIToolBar拥有的UIBarButtonItem实例,是由视图控制器进行管理的,如下所示:
 
工具栏风格:

typedef NS_ENUM(NSInteger, UIBarStyle) {

    UIBarStyleDefault          = 0,        //默认风格,蓝色文字

    UIBarStyleBlack            = 1,        //黑色背景,褐色文字

    UIBarStyleBlackOpaque      = 1,    // 纯黑色背景,白色文字

    UIBarStyleBlackTranslucent = 2,   // 透明黑色背景,白色文字

};

 
属性:

@property(nonatomic)        UIBarStyle barStyle;    //工具栏风格,默认为蓝色

@property(nonatomic,copy)   NSArray   *items;     //工具栏中的按钮单元,UIBarButtonItem

@property(nonatomic,assign,getter=isTranslucent) BOOL translucent  //是否透明

@property(nonatomic,retain) UIColor *tintColor;        //按钮颜色

@property(nonatomic,retain) UIColor *barTintColor; //工具栏颜色

方法:
※设置工具栏中的按钮单元

- (void)setItems:(NSArray *)items animated:(BOOL)animated;  

※设置工具栏的背景图像

- (void)setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;

※获取工具栏的背景图像

- (UIImage *)backgroundImageForToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics; 

※设置工具栏的阴影图像

- (void)setShadowImage:(UIImage *)shadowImage forToolbarPosition:(UIBarPosition)topOrBottom;

 ※获取工具栏的阴影图像

- (UIImage *)shadowImageForToolbarPosition:(UIBarPosition)topOrBottom ;

 

 

二、工具栏按钮控件:UIBarButtonItem:UIBarItem

属性:

@property(nonatomic)         UIBarButtonItemStyle style;            //风格

@property(nonatomic)         CGFloat              width;                // 调节间距宽度

@property(nonatomic,copy)    NSSet               *possibleTitles;   // 标题

@property(nonatomic,retain)  UIView              *customView;       // 自定义视图

@property(nonatomic)         SEL                  action;                // 事件

@property(nonatomic,assign)  id                   target;     // 目标代理

 

按钮单元风格UIBarButtonItem:

typedef NS_ENUM(NSInteger, UIBarButtonItemStyle) {

    UIBarButtonItemStylePlain,        //普通风格

    UIBarButtonItemStyleBordered,  //有边界的风格

    UIBarButtonItemStyleDone,       //蓝色风格

};

系统自带的按钮:

typedef NS_ENUM(NSInteger, UIBarButtonSystemItem) {

    UIBarButtonSystemItemDone,            //确认按钮

    UIBarButtonSystemItemCancel,          //取消按钮

    UIBarButtonSystemItemEdit,             //编辑按钮

    UIBarButtonSystemItemSave,            // 保存按钮

    UIBarButtonSystemItemAdd,             //添加按钮

    UIBarButtonSystemItemFlexibleSpace,//自动调节间距按钮

    UIBarButtonSystemItemFixedSpace,   //自定义调节间距按按钮

    UIBarButtonSystemItemCompose,      

    UIBarButtonSystemItemReply,          

    UIBarButtonSystemItemAction,

    UIBarButtonSystemItemOrganize,

    UIBarButtonSystemItemBookmarks,

    UIBarButtonSystemItemSearch,

    UIBarButtonSystemItemRefresh,

    UIBarButtonSystemItemStop,

    UIBarButtonSystemItemCamera,

    UIBarButtonSystemItemTrash,

    UIBarButtonSystemItemPlay,

    UIBarButtonSystemItemPause,

    UIBarButtonSystemItemRewind,

    UIBarButtonSystemItemFastForward,

    UIBarButtonSystemItemUndo,

    UIBarButtonSystemItemRedo,

    UIBarButtonSystemItemPageCurl,

};

主要方法:

※用图像初始化

- (instancetype)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;

图片(包括竖屏和横屏显示不同的图片) 

- (instancetype)initWithImage:(UIImage *)image landscapeImagePhone:(UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;

※用字符串初始化

- (instancetype)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;

※用系统按钮初始化

- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;

※用自定义图像初始化

- (instancetype)initWithCustomView:(UIView *)customView;

 

举例举例如下:

 1.在工具栏中全部添加系统自带的按钮单元:

复制代码
   //创建toolBal导航栏实例
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 667-60, 375, 40)];
    
    //设置toolBar风格
    toolbar.barStyle =  UIBarStyleBlack;
    
    //将该控件添加到视图中
    [self.view addSubview:toolbar];
    
    //创建控件上的按钮单元
    UIBarButtonItem *additem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
    
    UIBarButtonItem *edititem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:nil];
    
    UIBarButtonItem *titleitem = [[UIBarButtonItem alloc]initWithTitle:@"title" style:UIBarButtonItemStyleDone target:self action:nil];
    
    //创建灵活调节按钮单元,设置间隔
    UIBarButtonItem *flexibleitem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil];
    
    //将按钮单元都添加到数组中
    NSArray *items = @[additem,flexibleitem,edititem,flexibleitem,titleitem];
    
    
    //设置导航栏上的按钮单元
    [toolbar setItems:items animated:YES];
复制代码

演示结果如下:

 

2.在工具栏上添加图像按钮

复制代码
   //创建toolBal导航栏实例
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 30, 375, 40)];
    
    //设置toolBar风格
    toolbar.barStyle =  UIBarStyleDefault;
    
    //将该控件添加到视图中
    [self.view addSubview:toolbar];
    
    //创建控件上的按钮单元
    UIBarButtonItem *imageitem1 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"1.png"] style:UIBarButtonItemStylePlain target:self action:nil];
    
    UIBarButtonItem *imageitem2 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"2.png"] style:UIBarButtonItemStylePlain target:self action:nil];
    
    UIBarButtonItem *imageitem3 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"3.png"] style:UIBarButtonItemStylePlain target:self action:nil];
    
    //创建灵活调节按钮单元,设置间隔
    UIBarButtonItem *flexibleitem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil];
    
    //将按钮单元都添加到数组中
    NSArray *items = @[imageitem1,flexibleitem,imageitem2,flexibleitem,imageitem3];
    
    
    //设置导航栏上的按钮单元
    [toolbar setItems:items animated:YES];
复制代码

演示结果如下:

 

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
分类:  iOS初级

本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4850939.html,如需转载请自行联系原作者
相关文章
|
iOS开发
iOS MFMessageComposeViewController不显示取消按钮,导航条上白色,无取消按钮,无法返回应用...
iOS MFMessageComposeViewController不显示取消按钮,导航条上白色,无取消按钮,无法返回应用...
94 0
|
2月前
|
JavaScript 前端开发 iOS开发
ios样式开关按钮jQuery插件
ios样式开关按钮jQuery插件
63 7
|
3月前
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户点击按钮时,按钮将从圆形变为椭圆形,颜色从蓝色渐变到绿色;释放按钮时,动画以相反方式恢复。通过UIView的动画方法和弹簧动画效果,实现平滑自然的过渡。
98 1
|
4月前
|
Swift iOS开发 UED
如何使用Swift和UIKit在iOS应用中实现自定义按钮动画
【10月更文挑战第18天】本文通过一个具体案例,介绍如何使用Swift和UIKit在iOS应用中实现自定义按钮动画。当用户按下按钮时,按钮将从圆形变为椭圆形并从蓝色渐变为绿色;释放按钮时,动画恢复原状。通过UIView的动画方法和弹簧动画效果,实现平滑自然的动画过渡。
83 5
|
9月前
|
iOS开发
iOS13.6.1系统XR手机图文按钮显示不全问题
iOS13.6.1系统XR手机图文按钮显示不全问题
91 0
|
9月前
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
69 0
|
iOS开发
iOS 多个滚动控件嵌套Demo
iOS 多个滚动控件嵌套Demo
92 0
|
iOS开发
iOS 常用的 上下左右 拉刷新控件
iOS 常用的 上下左右 拉刷新控件
123 0
|
监控 Android开发 iOS开发
Android6.0 源码修改之 仿IOS添加全屏可拖拽浮窗返回按钮
Android6.0 源码修改之 仿IOS添加全屏可拖拽浮窗返回按钮
163 0
|
iOS开发
iOS短信验证码控件,自动输入回调两次解决办法
iOS短信验证码控件,自动输入回调两次解决办法
539 0

热门文章

最新文章

  • 1
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
  • 2
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 3
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 4
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 5
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 6
    iOS各个证书生成细节
  • 7
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
  • 8
    解决 input 输入框在 iOS 系统中无法输入内容
  • 9
    iOS:应用程序的线程安全性
  • 10
    iOS开发-UIScrollView原理
  • 1
    iOS|记一名 iOS 开发新手的前两次 App 审核经历
    10
  • 2
    iOS各个证书生成细节
    23
  • 3
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
    122
  • 4
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
    40
  • 5
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    55
  • 6
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
    46
  • 7
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
    40
  • 8
    uniapp开发ios打包Error code = -5000 Error message: Error: certificate file(p12) import failed!报错问题如何解决
    155
  • 9
    【05】2025年1月首发完整版-篇幅较长-苹果app如何上架到app store完整流程·不借助第三方上架工具的情况下无需花钱但需仔细学习-优雅草央千澈详解关于APP签名以及分发-们最关心的一篇来了-IOS上架app
    312
  • 10
    app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
    107