IOS开发学习笔记023-UIToolBar的使用
这里使用代码实现
大概过程:
1、创建工具条
2、创建插入条
3、添加头像、标签、删除按钮
4、点击头像获取标签信息
做一个简单的联系人列表,可以添加删除联系人,现在还没有添加头像和文字,接下来慢慢添加

1、如何在UIToolBar两头出现两个按钮bar button item
可是在按钮中间添加一个bar button item,然后设置按钮的属性Identifier为Flexible Space

2、然后拖拽添加事件
- (IBAction)add:(UIBarButtonItem *) sender; // 添加
- (IBAction)remove:(UIBarButtonItem *) sender; // 删除
3、实现消息响应
点击添加按钮后向视窗中添加一条联系人信息,每次都添加到最后边。
点击删除按钮从视窗中删除一条联系人信息,每次都先删除最后一条。当只剩下工具条时使删除按钮失效。
添加按钮
- (IBAction)add:(UIBarButtonItem *) sender // 添加
{
NSLog(@"%@",@"add");
// 0.获取最后一个控件
UIView *last = [self.view.subviews lastObject];
//计算当前要插入的控件的位置
// y = 最后一个控件的y + 最后一个控件的高度 + 间距
CGFloat rowY = last.frame.origin.y + last.frame.size.height + ; // 1.创建
UIView *view = [[UIView alloc] init];
//view.frame = CGRectMake(0,rowY,320,50);
view.backgroundColor = [UIColor redColor]; // 2.添加
[self.view addSubview:view]; // 3.删除按钮有效
_removeItem.enabled = YES; view.frame = CGRectMake(, rowY, , );
view.alpha = ;
// 4.执行动画
// 动画实现方式1
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:1.0]; // 动画实现方式2
//使用UIView的方法实现动画block
// [UIView animateWithDuration:1.0 animations:^{
//
// }];
// 使用UIView的方法实现动画block
[UIView animateWithDuration:kDur animations:^{
view.frame = CGRectMake(, rowY, , );
view.alpha = ;
// 在动画结束后添加其他操作
} completion:^(BOOL finished) {
NSLog(@"%@",@"add完毕");
}]; // [UIView commitAnimations];
}
其中的动画实现方式有三种:
1、动画实现方式1
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
// 出现动画
[UIView commitAnimations];
2、[UIView animateWithDuration:1.0 animations:^{
// 动画改变
}];
3、 [UIView animateWithDuration:kDur animations:^{
view.frame = CGRectMake(0, rowY, 320, 50);
view.alpha = 1;
}//这里在动画结束实现一些其他操作
completion:^(BOOL finished) {
NSLog(@"%@",@"add完毕");
}];
删除按钮
#pragma mark 删除一行
- (IBAction)remove:(UIBarButtonItem *) sender // 删除
{
NSLog(@"%@",@"remove"); // 1.取出最后一个子控件
UIView *last = [self.view.subviews lastObject];
// 判断是否是toolbar
//if([last isKindOfClass:[last class]]) return;//
[UIView animateWithDuration:kDur animations:^{
CGRect temp = last.frame;
temp.origin.x = ;
last.frame = temp; // 改变位置
last.alpha = ; // 改变透明度
} completion:^(BOOL finished) {
// 2.删除子控件
[last removeFromSuperview];
_removeItem.enabled = self.view.subviews.count != ;
NSLog(@"%@",@"remove完毕");
}]; // 3.判断剩下的子控件的个数
// if(self.view.subviews.count == 1)
// {
// _removeItem.enabled = NO;
// } }
4、给添加的子控件添加头像和标签
把add方法中得创建子控件分离出来到一个方法中
UIView *view = [self createRowView]; // 在原处替换为这句话
createRowView方法,常见一行信息包括头像和文字信息
// 创建一行
- (UIView *)createRowView
{
// 1 创建父控件
UIView *view = [[UIView alloc] init];
//view.frame = CGRectMake(0,rowY,320,50);
view.backgroundColor = [UIColor redColor]; // 2 添加标签
UILabel *lab = [[UILabel alloc] init];
lab.frame = CGRectMake(, , self.view.frame.size.width, );
// lab.center = CGPointMake(160, kRowH/2);
// 随机获取已经存在的几个名字
int nameIndex = arc4random_uniform([_allNames count]);
// 设置文字
lab.text = _allNames[nameIndex];
// 设置背景
lab.backgroundColor = [UIColor clearColor];
// 设置文本居中显示
lab.textAlignment = NSTextAlignmentCenter; // 枚举常量
// 设置tag属性
[lab setTag:kTag];
// 添加到父控件
[view addSubview:lab]; // 3 添加头像
UIButton *icon = [UIButton buttonWithType:UIButtonTypeCustom];
icon.frame = CGRectMake(, , , kRowH);
// 随机产生文件名 ,0~8
int randomindex = arc4random() % ;
//int randomindex2 = arc4random_uniform(9);
NSString *name = [NSString stringWithFormat:@"01%d.png",randomindex];
// 设置图片
[icon setImage:[UIImage imageNamed:name] forState:UIControlStateNormal]; // 添加监听器,响应按钮按下事件
[icon addTarget:self action:@selector(iconClick:) forControlEvents:UIControlEventTouchUpInside];
// 添加到父控件
[view addSubview:icon]; return view;
}
1 // 监听器
- (void)iconClick:(UIButton *)btn
{
//NSLog(@"%@",btn);
// 根据tag获取父控件内部子控件tag为10的子控件
UILabel *lab = (UILabel *)[btn.superview viewWithTag:]; NSLog(@"%@",lab.text);
}
添加的数组在这里
// 类扩展(class extension 匿名分类)
// 添加类的私有属性和方法
@interface SLQViewController ()
{
NSArray *_allNames;
}
@end @implementation SLQViewController #pragma mark 控制器的view调用完毕调用一次
- (void)viewDidLoad
{
[super viewDidLoad]; _allNames = @[@"张三",@"李四",@"万物",@"傻缺"];
}
现在效果如下,可添加一行,删除一行,并随机显示头像和标签

5、给每一条信息都添加删除按钮
在创建时直接添加上按钮,写在添加过头像之后
// 4.删除按钮
UIButton *del = [UIButton buttonWithType:UIButtonTypeRoundedRect];
del.frame = CGRectMake(, , , );
del.backgroundColor = [UIColor yellowColor];
[del setTitle:@"删除" forState:UIControlStateNormal];
// 添加监听事件
[del addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside]; [view addSubview:del];
然后响应按下事件
// 监听删除按钮
- (void)deleteClick:(UIButton *)del
{
// NSLog(@"%f",del.superview.frame.origin.y);
// 添加动画效果
[UIView animateWithDuration:kDur animations:^{
// 改变位置
CGRect temp = del.superview.frame;
temp.origin.x = kWidth;
del.superview.frame = temp; del.superview.alpha = ; // 透明
}
// 动画执行结束后执行这个
completion:^(BOOL finished)
{
// 1 获取当前要删除控件的索引
int index = [self.view.subviews indexOfObject:del.superview];
// 2 删除当前行
[del.superview removeFromSuperview]; [UIView animateWithDuration:kDur-0.3 animations:^{
// 3 删除后后面控件上移
for (int i = index; i < self.view.subviews.count; i ++)
{
//
UIView *child = self.view.subviews[i];
CGRect temp = child.frame;
temp.origin.y -= kRowH + ;
child.frame = temp;
}
}]; // 4 判断删除按钮是否失效
_removeItem.enabled = self.view.subviews.count > ; }]; }
代码
//
// SLQViewController.m
// 联系人管理
//
// Created by Christian on 15/5/13.
// Copyright (c) 2015年 itcast. All rights reserved.
// #import "SLQViewController.h" #define kDur 1.0
#define kRowH 50
#define kTag 10
#define kWidth 320
// 类扩展(class extension 匿名分类)
// 添加类的私有属性和方法
@interface SLQViewController ()
{
NSArray *_allNames;
}
@end @implementation SLQViewController #pragma mark 控制器的view调用完毕调用一次
- (void)viewDidLoad
{
[super viewDidLoad]; _allNames = @[@"张三",@"李四",@"万物",@"傻缺"];
}
#pragma mark 添加一行
- (IBAction)add:(UIBarButtonItem *) sender // 添加
{
NSLog(@"%@",@"add");
// 0.获取最后一个控件
UIView *last = [self.view.subviews lastObject];
//计算当前要插入的控件的位置
// y = 最后一个控件的y + 最后一个控件的高度 + 间距
CGFloat rowY = last.frame.origin.y + last.frame.size.height + ; // 1.创建
// UIView *view = [[UIView alloc] init];
// //view.frame = CGRectMake(0,rowY,320,50);
// view.backgroundColor = [UIColor redColor];
UIView *view = [self createRowView];
// 2.添加
[self.view addSubview:view]; // 3.删除按钮有效
_removeItem.enabled = YES; view.frame = CGRectMake(, rowY, , );
view.alpha = ;
// 4.执行动画
// 动画实现方式1
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:1.0]; // 动画实现方式2
//使用UIView的方法实现动画block
// [UIView animateWithDuration:1.0 animations:^{
//
// }];
// 使用UIView的方法实现动画block
[UIView animateWithDuration:kDur animations:^{
view.frame = CGRectMake(, rowY, , );
view.alpha = ; } completion:^(BOOL finished) {
NSLog(@"%@",@"add完毕");
}]; // [UIView commitAnimations];
} // 创建一行
- (UIView *)createRowView
{
// 1 创建父控件
UIView *view = [[UIView alloc] init];
//view.frame = CGRectMake(0,rowY,320,50);
view.backgroundColor = [UIColor redColor]; // 2 添加标签
UILabel *lab = [[UILabel alloc] init];
lab.frame = CGRectMake(, , self.view.frame.size.width, );
// lab.center = CGPointMake(160, kRowH/2);
// 随机获取已经存在的几个名字
int nameIndex = arc4random_uniform([_allNames count]);
// 设置文字
lab.text = _allNames[nameIndex];
// 设置背景
lab.backgroundColor = [UIColor clearColor];
// 设置文本居中显示
lab.textAlignment = NSTextAlignmentCenter; // 枚举常量
// 设置tag属性
[lab setTag:kTag];
// 添加到父控件
[view addSubview:lab]; // 3 添加头像
UIButton *icon = [UIButton buttonWithType:UIButtonTypeCustom];
icon.frame = CGRectMake(, , , kRowH);
// 随机产生文件名 ,0~8
int randomindex = arc4random() % ;
//int randomindex2 = arc4random_uniform(9);
NSString *name = [NSString stringWithFormat:@"01%d.png",randomindex];
// 设置图片
[icon setImage:[UIImage imageNamed:name] forState:UIControlStateNormal]; // 添加监听器
[icon addTarget:self action:@selector(iconClick:) forControlEvents:UIControlEventTouchUpInside];
// 添加到父控件
[view addSubview:icon]; // 4.删除按钮
UIButton *del = [UIButton buttonWithType:UIButtonTypeRoundedRect];
del.frame = CGRectMake(, , , );
del.backgroundColor = [UIColor yellowColor];
[del setTitle:@"删除" forState:UIControlStateNormal];
// 添加监听事件
[del addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside]; [view addSubview:del]; return view;
}
// 监听删除按钮
- (void)deleteClick:(UIButton *)del
{
// NSLog(@"%f",del.superview.frame.origin.y);
// 添加动画效果
[UIView animateWithDuration:kDur animations:^{
// 改变位置
CGRect temp = del.superview.frame;
temp.origin.x = kWidth;
del.superview.frame = temp; del.superview.alpha = ; // 透明
}
// 动画执行结束后执行这个
completion:^(BOOL finished)
{
// 1 获取当前要删除控件的索引
int index = [self.view.subviews indexOfObject:del.superview];
// 2 删除当前行
[del.superview removeFromSuperview]; [UIView animateWithDuration:kDur-0.4 animations:^{
// 3 删除后后面控件上移
for (int i = index; i < self.view.subviews.count; i ++)
{
//
UIView *child = self.view.subviews[i];
CGRect temp = child.frame;
temp.origin.y -= kRowH + ;
child.frame = temp;
}
}]; // 4 判断删除按钮是否失效
_removeItem.enabled = self.view.subviews.count > ; }]; } // 监听头像按钮
- (void)iconClick:(UIButton *)btn
{
//NSLog(@"%@",btn);
// 根据tag获取父控件内部子控件tag为10的子控件
UILabel *lab = (UILabel *)[btn.superview viewWithTag:]; NSLog(@"%@",lab.text);
} #pragma mark 删除一行
- (IBAction)remove:(UIBarButtonItem *) sender // 删除
{
NSLog(@"%@",@"remove"); // 1.取出最后一个子控件
UIView *last = [self.view.subviews lastObject];
// 判断是否是toolbar
//if([last isKindOfClass:[last class]]) return;//
[UIView animateWithDuration:kDur animations:^{
CGRect temp = last.frame;
temp.origin.x = ;
last.frame = temp; // 改变位置
last.alpha = ; // 改变透明度
} completion:^(BOOL finished) {
// 2.删除子控件
[last removeFromSuperview];
_removeItem.enabled = self.view.subviews.count != ;
NSLog(@"%@",@"remove完毕");
}]; // 3.判断剩下的子控件的个数
// if(self.view.subviews.count == 1)
// {
// _removeItem.enabled = NO;
// } } @end
IOS开发学习笔记023-UIToolBar的使用的更多相关文章
- iOS开发学习笔记:基础篇
iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...
- ios开发学习笔记(1)
objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = ...
- ios开发学习笔记(这里一定有你想要的东西,全部免费)
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- iOS开发学习笔记
1 常用的第三方工具 1.1 iPhone Simulator 测试程序需要模拟器iPhone Simulator 1.2 设计界面需要Interface Builder,Interface Buil ...
- IOS开发学习笔记017-第一个IOS应用
第一个IOS应用程序,就从最简单的开始吧. 1.先了解一下开发环境,Xcode的相关组成 2.还有模拟器 3.运行与停止按钮 4.新建一个工程 5.看看main函数里都有啥 6.现在来添加一个控件 1 ...
- (ios开发学习笔记一)ios项目文件结构
转自:http://www.cnblogs.com/macroxu-1982/archive/2012/07/31/2616389.html 下面是单个窗体项目例子,我们从这个项目开始,说明ios项目 ...
- ios开发学习笔记040-autolayout 第三方框架Masonry
不管是是界面创建约束还是代码创建约束,苹果官方提供的方式都比较繁琐.所以出现了第三方框架. Masonry 在github地址如下: https://github.com/SnapKit/Masonr ...
- IOS开发学习笔记033-UIScrollView
1.滚动显示图片 如果图片过大,则需要滚动显示,这是需要用到类UIScrollView,可是实现控件的水平和垂直滚动. 可用三步实现:1 设置UIScrollView,2 设置UIImageView, ...
- IOS开发学习笔记026-UITableView的使用
UITableView的简单使用过程 简单介绍 两种样式 UITableViewStylePlain UITableViewStyleGrouped 数据显示需要设置数据源,数据源是符合遵守协议 &l ...
随机推荐
- File 与 Log #3--动态加入控件,[图片版]访客计数器(用.txt档案来记录)
File 与 Log #3--动态加入控件,[图片版]访客计数器(用.txt档案来记录) 以前的两篇文章(收录在书本「上集」的第十七章) 请看「ASP.NET专题实务」,松岗出版 File 与 Log ...
- [转]iOS开发总结之代码规范
转自:http://www.cocoachina.com/ios/20151014/13678.html 命名规范 总 的来说, iOS命名两大原则是:可读性高和防止命名冲突(通过加前缀来保证). O ...
- 知乎日报客户端应用ios源码
swift开发的知乎日报客户端详细源码,里面分UI和网络两个模块. 1.涉及到了大部分的UI控件的使用(甚至包括UIRefreshView,UITableConrol等等)2.Connection完成 ...
- IOS 九宫图解锁(封装)
NJLockView.h /.m @class NJLockView; @protocol NJLockViewDelegate <NSObject> - (void)lockViewDi ...
- 【BZOJ1057】[ZJOI2007] 棋盘制作(单调栈的运用)
点此看题面 大致题意: 给你一个\(N*M\)的\(01\)矩阵,要求你分别求出最大的\(01\)相间的正方形和矩形(矩形也可以是正方形),并输出其面积. 题解 这题第一眼看去没什么思路,仔细想想,能 ...
- Angular2--显示数据
1.插值表达式 要显示组件的属性,最简单的方式就是通过插值表达式来绑定属性名.要使用插值表达式,就把属性名包裹在双花括号里放进视图模板,如 {{}} eg: <h1>{{ name }}& ...
- eclipse中的字体大小设置和背景色设置
1.字体大小设置 在basic下选择最后一个TextFont 护眼背景色设置 添加到自定义颜色后点确定 最后一步点apply
- cf519C. A and B and Team Training(找规律)
题意 $a$个学生,$b$个教练 可以两个学生和一个教练一组,也可以两个教练和一个学生一组,问最多组成多少组 Sol 发题解的目的是为了纪念一下自己的错误思路 刚开始想的是:贪心的选,让少的跟多的分在 ...
- BZOJ3398: [Usaco2009 Feb]Bullcow 牡牛和牝牛(dp)
题意 约翰要带N(1≤N≤100000)只牛去参加集会里的展示活动,这些牛可以是牡牛,也可以是牝牛.牛们要站成一排.但是牡牛是好斗的,为了避免牡牛闹出乱子,约翰决定任意两只牡牛之间至少要有K( ...
- select * 比select column快很多奇怪案例分析
遇到MYSQL傻傻的地方,下面给个案例,大家感受下: 注意以下两个sql只有select *和select g.id区别. SQL1:SELECT g.idFROM table1 gINNER JOI ...