一、工具栏控件: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(, -, , )]; //设置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的使用的更多相关文章

  1. Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子

    Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子 procedure TForm1.Button1Click(Sender: TObject); ...

  2. iOS 开发 中级:UIToolbar,UINavigationBar,UITabBar,UIBarButtonItem,UITabBarItem自定义方法总结

    原文:  http://blog.csdn.net/songrotek/article/details/8692866?utm_source=tuicool 对于UIToolbar,UINavigat ...

  3. Android控件之Button(按钮控件)和ImageButton(图片按钮控件)

    一.Button和ImageButton特证: 1.共同特证: 都可以作为一个按钮产生点击事件 2.不同特证: Button有text的属性,ImageButton没有 ImageButton有src ...

  4. MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)

    本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box)等.命令按钮就是我们前面多次提到的侠义的 ...

  5. VS2010/MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)

    言归正传,鸡啄米上一节中讲了编辑框的用法,本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box ...

  6. [Xcode 实际操作]三、视图控制器-(9)在Storyboard中使用标签和按钮控件

    目录:[Swift]Xcode实际操作 本文将演示标签和按钮在故事板中的应用. 在欢迎串口中,点击创建一个新的项目[Create a new Xcode project] [Single View A ...

  7. WindowsForm 公共控件 菜单和工具栏

                                                      公共控件   菜单栏 状态栏   布局    公共控件 textbox:  text属性:用于获取或 ...

  8. FineUI第五天---按钮控件

    按钮控件 <x:Button runat="server" ID="按下" Text="按下"></x:Button> ...

  9. IOS学习笔记(四)之UITextField和UITextView控件学习

    IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...

随机推荐

  1. Linux Python apache的cgi配置

    一.找到安装Apache的目录/usr/local/apache2/conf,并对httpd.conf配置文件进行修改 1.加载cgi模块 去掉注释: LoadModule cgid_module m ...

  2. textarea在浏览器中固定大小

    HTML 标签 textarea 在大部分浏览器中只要指定行(rows)和列(cols)属性,就可以规定 textarea 的尺寸,大小就不会改变,不过更好的办法是使用 CSS 的 height 和 ...

  3. CSS 绝对居中方案

    .Absolute-Center { margin: auto; position: absolute; top:;;;; }

  4. Druid数据迁移小计

    Druid数据迁移小计 Druid 官方网站上讲了相关的 Dump Segment 和 Insert Segment 相关的功能,但是经过测试这些功能都不好用,报 Guice 的依赖错误,懒得找具体原 ...

  5. 洛谷—— P1908 逆序对

    https://www.luogu.org/problem/show?pid=1908 题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏, ...

  6. 多进程失败拉起的demo

    #include <iostream> #include <vector> #include <unistd.h> #include <stdlib.h> ...

  7. 【java回调】同步/异步回调机制的原理和使用方法

    回调(callback)在我们做工程过程中经常会使用到,今天想整理一下回调的原理和使用方法. 回调的原理可以简单理解为:A发送消息给B,B处理完后告诉A处理结果.再简单点就是A调用B,B调用A. 那么 ...

  8. Codeforces 961E 主席树

    题意: 给出一个n个数的序列,求有几对(i,j)满足a[i]>=j&&a[j]>=i,(i,j)和(j,i)只能算一对. 考虑第i个数会有几个j(j<i)满足条件,首 ...

  9. ajax 同步和异步的区别

    举个例子:普通B/S模式(同步)AJAX技术(异步)同步:提交请求->等待服务器处理->处理完毕返回 这个期间客户端浏览器不能干任何事异步: 请求通过事件触发->服务器处理(这时浏览 ...

  10. [转]MySQL 数据类型(二)

    MySQL 的数值数据类型可以大致划分为两个类别,一个是整数,另一个是浮点数或小数.许多不同的子类型对这些类别中的每一个都是可用的,每个子类型支持不同大小的数据,并且 MySQL 允许我们指定数值字段 ...