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


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; // 目标代理
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(, -, , )]; //设置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(, , , )]; //设置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: 工具栏控件UIToolBar和工具栏按钮控件UIBarButtonItem的使用的更多相关文章
- Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子
Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子 procedure TForm1.Button1Click(Sender: TObject); ...
- iOS 开发 中级:UIToolbar,UINavigationBar,UITabBar,UIBarButtonItem,UITabBarItem自定义方法总结
原文: http://blog.csdn.net/songrotek/article/details/8692866?utm_source=tuicool 对于UIToolbar,UINavigat ...
- Android控件之Button(按钮控件)和ImageButton(图片按钮控件)
一.Button和ImageButton特证: 1.共同特证: 都可以作为一个按钮产生点击事件 2.不同特证: Button有text的属性,ImageButton没有 ImageButton有src ...
- MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box)等.命令按钮就是我们前面多次提到的侠义的 ...
- VS2010/MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
言归正传,鸡啄米上一节中讲了编辑框的用法,本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box ...
- [Xcode 实际操作]三、视图控制器-(9)在Storyboard中使用标签和按钮控件
目录:[Swift]Xcode实际操作 本文将演示标签和按钮在故事板中的应用. 在欢迎串口中,点击创建一个新的项目[Create a new Xcode project] [Single View A ...
- WindowsForm 公共控件 菜单和工具栏
公共控件 菜单栏 状态栏 布局 公共控件 textbox: text属性:用于获取或 ...
- FineUI第五天---按钮控件
按钮控件 <x:Button runat="server" ID="按下" Text="按下"></x:Button> ...
- IOS学习笔记(四)之UITextField和UITextView控件学习
IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...
随机推荐
- MiCode 40: 找小“3”
题目链接 这道题真的是zjb恶心, 看其起来像是个数位dp, 然而我并不会数位dp.然后就xjb乱写了个雷类似于动态规划的玩意, 然后调出了\(9\times 9 = 81\)种Bug, 终于过了. ...
- css文件放在头部的原因
我在博问上发的一个这个问题 然后有人这样回复我的 我感觉很有道理的样子 所以我放上来了 这样会先加载css的样式,在渲染dom的时候已经知道了自己的样式了,所以一次渲染成功 如果css放在底部,那么需 ...
- Django如何使文件在django自动执行
1. Django admin源码中 admin.py from django.contrib import admin # Register your models here. 在admin源码中 ...
- Django web框架之权限管理一
1. 需求分析: 准备:创建独立app, rbac #权限管理模块/组件 app01 #应用 分配权限,URL 2. 数据库设计 2.1 设计思路 第一版: 权限表: ID url title is_ ...
- 深度学习方法:受限玻尔兹曼机RBM(四)对比散度contrastive divergence,CD
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入 上篇讲到,如果用Gibbs Sa ...
- 关于在C#对类的属性理解
在类中都有一些成员.什么是类中的成员呢,我个人理解的是一个类中所应有的属性,方法,字段(因为目前才接触到类.所以类中一些其它应有的东西还不太熟悉),现在只探讨我列举的这几个在类中应有的东西.什么是属性 ...
- 前端读者 | 如何判断Javascript对象是否存在
本文来自@阮一峰 Javascript语言的设计不够严谨,很多地方一不小心就会出错. 举例来说,请考虑以下情况. 现在,我们要判断一个全局对象myObj是否存在,如果不存在,就对它进行声明.用自然语言 ...
- Power BI连接至Amazon Redshift
一直在使用Power BI连接至MongoDB中,但效果一直不是太理想,今天使用另一种方法,将MongoDB中的数据通过Azure Data Factory转入Amazon Redshift中,而在P ...
- linux下LD_PRELOAD的用处
linux下LD_PRELOAD的用处 在UNIX的动态链接库的世界中,LD_PRELOAD就是这样一个环境变量,它可以影响程序的运行时的链接(Runtime linker),它允许你定义在程序运行前 ...
- 可持久化01Trie树+LCA【p4592】[TJOI2018]异或
Description 现在有一颗以\(1\)为根节点的由\(n\)个节点组成的树,树上每个节点上都有一个权值\(v_i\).现在有\(Q\)次操作,操作如下: 1\(\;x\;y\):查询节点\(x ...