UIToolBar控件:是经常使用的一个工具条控件,虽然在上面可以添加子控件,但是toolbar中只能添加UIBarButtonItem类型的子控件,其他子控件会被包装成这种类型的,例如UIButton。通过工具栏可以用来对视图View中内容进行操作。

原理:

可以在toolBar上添加任何子控件。其实它的原理是把你要添加的子控件先加到toolbarItems数组里面,最后再把toolbarItems数组一次性放到toolbar工具栏里面。

虽然可以在toolbar中添加其他任何的视图控件如UILabel、UITextField、UIImageView等等,但是在xib/storyboard图形界面设计时,不能它们直接放置到UIToolBar中。若强行将它们拖曳到UIToolBar,会使它们放置在上层容器中,而不是UIToolBar中。所以前提是先必须添加一个视图UIView控件到toolbar中,它会被包装成UIBarButtonItem类型,然后再在UIView中添加需要的子视图控件。

举例如下:将UILabel加入到toolbar工具栏中,步骤如下:

1. 将UIView拖曳到UIToolBar(UIToolBar中自动增加了一个UIBarButtonItem,其中便是刚才插入的UIView);

2. 将UILabel(或其他控件)拖曳到刚才的UIView中;

3. 将刚才的UIView的Background设为某种颜色(如蓝色);

4. 将刚才的UIView的Background设为Default。

对toolbar进行初始化:

-initWithTitle(添加button用这个)

-initWithImage

-initWithBarButtonSystemItem(添加系统自定义的button,形状跟大小都已经固定了)下面链接里面有按钮图片样式

-initWithCustomView(添加除了button以外的View)

一、采用系统默认.xib文件中的UIToolBar,制作的工具栏(删除和添加图片)

(1)默认的View视图布局

  代码如下:需要在代码中为添加的控件人为设置frame具体坐标x,y、大小width,height

 #import "ViewController.h"
#define CONTACE_VIEW_HEIGHT 50
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButtonitemDelete; @end @implementation ViewController
- (IBAction)addContact:(UIBarButtonItem *)sender
{ //让删除按钮有效
[self.barButtonitemDelete setEnabled:YES]; //在subView中已经有了3个控件
NSInteger count = self.view.subviews.count - ;
CGRect lastFrame = self.toolBar.frame; UIView *contactView = [[UIView alloc]init];
CGFloat gapY = ;
CGFloat x = ;
CGFloat y = lastFrame.origin.y + lastFrame.size.height + (CONTACE_VIEW_HEIGHT+gapY) * count;
CGFloat w = self.view.frame.size.width; contactView.frame = CGRectMake(x,y,w,CONTACE_VIEW_HEIGHT); //添加头像
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",arc4random_uniform()]];
UIImageView *face = [[UIImageView alloc]initWithImage:image];
face.frame = CGRectMake(, ,,);
[contactView addSubview:face]; //添加姓名
UILabel *labelName = [[UILabel alloc]init];
labelName.text = [NSString stringWithFormat:@"name%d",arc4random_uniform()];
labelName.frame = CGRectMake(+image.size.width+, , , );
[contactView addSubview:labelName]; contactView.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:contactView];
}
- (IBAction)deleteContact:(UIBarButtonItem *)sender
{
//删除视图
UIView *lastView = [self.view.subviews lastObject];
[lastView removeFromSuperview]; //如果没有了contactView,设置删除按钮无效
if(self.view.subviews.count == )
{
[self.barButtonitemDelete setEnabled:NO];
}
} - (void)viewDidLoad {
[super viewDidLoad];
//开始时删除设置为无效
[self.barButtonitemDelete setEnabled:NO];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

  二、采用提前自定义布局的.xib文件中的UIToolBar,制作的工具栏(删除和添加图片)

(2)自定义的View视图布局,添加UIImage、UILabel控件

    

    代码如下:不需要在代码中再去设置添加控件的frame,在.xib文件中已经布局好了。

 import "ViewController.h"
#define CONTACE_VIEW_HEIGHT 50
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButtonitemDelete; @end @implementation ViewController
- (IBAction)addContact:(UIBarButtonItem *)sender
{ //让删除按钮有效
[self.barButtonitemDelete setEnabled:YES]; //在subView中已经有了3个控件
NSInteger count = self.view.subviews.count - ;
CGRect lastFrame = self.toolBar.frame; //加载xib文件
NSArray *views = [[NSBundle mainBundle]loadNibNamed:@"contactView" owner:nil options:nil]; //添加contactView
UIView *contactView = [views lastObject]; CGFloat gapY = ;
CGFloat x = ;
CGFloat y = lastFrame.origin.y + lastFrame.size.height + (CONTACE_VIEW_HEIGHT+gapY) * count;
CGFloat w = self.view.frame.size.width;
contactView.frame = CGRectMake(x,y,w,CONTACE_VIEW_HEIGHT); //添加头像
UIImageView *face = (UIImageView *)[contactView viewWithTag:];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",arc4random_uniform()]];
[face setImage:image]; //添加姓名
UILabel *labelName = (UILabel *)[contactView viewWithTag:];
labelName.text = [NSString stringWithFormat:@"name%d",arc4random_uniform()]; [self.view addSubview:contactView];
} - (IBAction)deleteContact:(UIBarButtonItem *)sender
{
//删除视图
UIView *lastView = [self.view.subviews lastObject];
[lastView removeFromSuperview]; //如果没有了contactView,设置删除按钮无效
if(self.view.subviews.count == )
{
[self.barButtonitemDelete setEnabled:NO];
}
} - (void)viewDidLoad {
[super viewDidLoad];
//开始时删除设置为无效
[self.barButtonitemDelete setEnabled:NO];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

iOS:UIToolBar控件的使用的更多相关文章

  1. JS调用Android、Ios原生控件

    在上一篇博客中已经和大家聊了,关于JS与Android.Ios原生控件之间相互通信的详细代码实现,今天我们一起聊一下JS调用Android.Ios通信的相同点和不同点,以便帮助我们在进行混合式开发时, ...

  2. 初识IOS,Label控件的应用。

    初识IOS,Label控件的应用. // // ViewController.m // Gua.test // // Created by 郭美男 on 16/5/31. // Copyright © ...

  3. IOS—UITextFiled控件详解

    IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...

  4. [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)

    A.概述      在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能      1.按钮点击后,显示为“已下载”,并且不 ...

  5. android 仿ios开关控件

    ios一些控件还是挺美丽的,可是对android程序猿来说可能比較苦逼,由于ios一些看起来简单的效果对android来说可能就没那么简单了,可是没办法非常多产品都是拿ios的一些控件叫android ...

  6. 79.iOS 设备的UI规范和iOS各控件默认高度

    iOS设备的UI 规范 iPhone界面尺寸 iPhone图标尺寸 iPad的设计尺寸 iPad图标尺寸 iPhone设备尺寸分辨率比例 iPhone各设备 launch image iOS 各种控件 ...

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

    一.工具栏控件:UIToolBar:UIView 介绍: ToolBar工具栏是视图View的属性,可以在工具栏上添加工具栏按钮Bar Button Item(可以是自定义的Custom.也可以是系统 ...

  8. iOS:UIResponser控件的介绍(响应者)

    UIResponser响应者控件   知识: 在iOS中不是任何对象都能处理事件,只有继承了UIResponser的对象才能接收并处理事件.我们称之为“响应者对象” UIApplication,UIV ...

  9. ios UI控件的简单整理

    把该文件拷贝到.m文件中就能够方便的查找 /** 匿名类目:能够声明方法和变量,属性为private(不同意在外部调用,且不能被继承 */ /** 发送数据的托付方,接收数据的时代理发(即代理的反向传 ...

随机推荐

  1. JavaScript中正则的使用(1)

    通过例子学习正则中的常见语法(1) $num javascript var a = 'javascript'; var b = a.replace(/(java)(script)/gi, '$2-$1 ...

  2. git推送本地仓库到github

    总结一下,方便后人,也方便自己查阅.直接写步骤 一.本地创建一个文件夹,在里面写项目的文件(* .php/*.js.....). git本地操作: 1 .  cd  /path/to/project  ...

  3. PHP模板引擎,Smarty定义

    PHP模板引擎:PHP是一种HTML内嵌式的在服务器端执行的脚本语言.初始的开发模板就是混合 层的数据编程,虽然通过MVC的设计模式可以实现将程序的应用逻辑与网页的呈现逻辑强制 分离,但也只是将程序的 ...

  4. Web安全漏洞深入分析及其安全编码

    摘自:http://blog.nsfocus.net/web-vulnerability-analysis-coding-security/ 超全Web漏洞详解及其对应的安全编码规则,包括:SQL注入 ...

  5. iview-cli 项目、iView admin 跨域问题解决方案

    在build 目录的 webpack.dev.config.js 目录中 module.exports = merge(webpackBaseConfig, { devtool: '#source-m ...

  6. 一文搞定 Mybatis 的应用

    Mybatis 介绍 Mybatis 是一个开源的持久层框架,原来叫 ibatis ,它对 jdbc 操作数据库的过程进行了封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动. ...

  7. load Properties

    /* */ public static final Properties loadProperties(String propertyFileRelativePath) /* */ { /* 67 * ...

  8. 1011 World Cup Betting (20)(20 point(s))

    problem With the 2010 FIFA World Cup running, football fans the world over were becoming increasingl ...

  9. django信号调度的用法

    Django中提供了"信号调度",用于在框架执行操作时解耦. 一些动作发生的时候,系统会根据信号定义的函数执行相应的操作 Django中内置的signal Model_signal ...

  10. 浏览器在线查看pdf文件 pdf.js的使用教程

    谷歌浏览器可以直接在线查看pdf,而IE内核浏览器无法在线查看,默认是下载. 这里用到的是pdf.js,不仅支持IE内核浏览器,而且兼容手机查看pdf 官网地址:http://mozilla.gith ...