iOS边练边学--通知机制和键盘处理小练习
一、发送通知
NSNotification *note = [NSNotification notificationWithName:@"通知的名称,随便写,例如:你妈叫你回家吃饭" object:self userInfo:@{@"time":@"11:30",
@"desc":@"回家吃饭"
}];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotification:note];
二、接收通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
三、键盘处理小练习
注意事项:tableView的高度约束取消,如果约束在的话,tableView无法实现上移,只能实现往上压缩
正确做法:设置tableView的高度约束等于父类约束的高度减去最下面工具栏的高度,如图:

<1>将键盘的显示和隐藏分开处理的情况
接收通知的代码:
- (void)viewDidLoad {
[super viewDidLoad];
// addObserver-谁来接收 @Selector-接收完成需要执行的方法,带一个参数,接收通知中心发来的NSNotofication对象
// object-发送消息的对象,nil-不管谁发送的,只要是name中的消息都接收,这里监听的是键盘的即将弹出的消息,系统发出的消息是字符串常亮
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// 接收键盘隐藏的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
接收到通知后的处理代码:
- (void)keyboardWillShow:(NSNotification *)note
{
// 取出时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 取出键盘显示出来后的最终frame
CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 改变约束
self.bottomSpacing.constant = rect.size.height;
// 动画效果
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
} - (void)keyboardWillHide:(NSNotification *)note
{
// 取出时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 约束改为0
self.bottomSpacing.constant = ;
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
<2>将键盘显示和隐藏一起处理的情况
- (void)viewDidLoad {
[super viewDidLoad];
// 接收通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
- (void)keyboardChange:(NSNotification *)note
{
// 取出时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 取出键盘最终的frame
CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
self.bottomSpacing.constant = [UIScreen mainScreen].bounds.size.height - rect.origin.y;
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
- (void)keyboardChange:(NSNotification *)note
{
// 取出时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 取出键盘最终的frame
CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
self.bottomSpacing.constant = [UIScreen mainScreen].bounds.size.height - rect.origin.y;
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
四、最后一步,也是最重要的一步,记得取消注册的通知监听器
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
iOS边练边学--通知机制和键盘处理小练习的更多相关文章
- iOS边练边学--通知机制和键盘处理
一.通知中心(NSNotificationCenter) 每一个程序都有一个通知中心实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以想通知中心发布通知(NSNotification),描述 ...
- iOS边练边学--Http网络再学习,简单介绍
一.URL 什么是URL URL中常见的协议 二.Http Http的基本通信过程 发送Http请求的方法 GET 和 POST 对比 GET 和 POST 的选择 三.iOS中的Http学习 iOS ...
- iOS边练边学--多线程介绍、NSThread的简单实用、线程安全以及线程之间的通信
一.iOS中的多线程 多线程的原理(之前多线程这块没好好学,之前对多线程的理解也是错误的,这里更正,好好学习这块) iOS中多线程的实现方案有以下几种 二.NSThread线程类的简单实用(直接上代码 ...
- iOS边练边学--CALayer,非根层隐式动画,钟表练习
一.CALayer UIView之所以能显示在屏幕上,完全是因为他内部的一个图层 在创建UIView对象时,UIView内部会自动创建一个图层(即CALayer对象),通过UIView的layer属性 ...
- iOS边练边学--UIGestureRecognizer手势识别器简单介绍
iOS 3.2之后,苹果退出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度. 一.UIGestureRecognizer UIGestureRe ...
- iOS边练边学--触摸事件以及能够拖拽的UIView的练习
一.用户在使用APP的过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: 二.响应者对象 在iOS中只有继承了了UIResponder的对象才能接受并处理事件,这样的对象称之为“响应者对象 ...
- iOS边练边学--应用数据存储的常用方式(plist,Preference,NSKeyedArchiver)其中的三种
iOS应用数据存储的常用方式: XML属性列表(plist)归档 Preference(偏好设置) NSKeyedArchiver归档(NSCoding) SQLite3--这里暂且不讲 Core D ...
- iOS边练边学--图片的拉伸
图片拉伸方法一: IOS 5.0以后才有的方法: - (void)viewDidLoad { [super viewDidLoad]; UIImageView *imageView = [[UIIma ...
- iOS边练边学--NSURLSessionDataTask实现文件真正的断点续传
实现重点: NSURLSessionDataTask要设置请求头,从路径中获取文件已经下载的长度(文件没有下载过的话,长度为0).通过这个长度设置请求的Range 如图: 接收到请求的时候key:文件 ...
随机推荐
- Android学习系列(19)--App离线下载
宜未雨而绸缪,毋临渴而掘井.----朱用纯<治家格言> 离线下载,在有网络的情况下下载服务器数据,以便无网络时也能阅读,就是离线阅读. 离线下载的功能点如下: ...
- WCF中可以使用SVCUtil.exe生成客户端代理类和配置文件
1.找到如下地址“C:\Windows\System32\cmd.exe” 命令行工具,右键以管理员身份运行(视系统是否为win7 而定) 2.输入如下命令: C:\>cd C ...
- js LINQ教程
在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...
- configure: error: newly created file is older than distributed files!
在linux下安装软件包的时候,有时候提示 configure: error: newly created file is older than distributed files!Check you ...
- 工作总结 MVC 验证 [Required] 必填 与 string 小知识
例如 添加页面有个 title 字段 设置了 [Required] 不填的时候 设置 还是验证不通过 设置为 还是不通过 说明了 验证只与页面上传不传值有关 与在后台设不设置值 无关. ...
- matplotlib热图
1.基础知识点回顾 1.plot(x, y, marker='D')表示绘制折线图,marker设置样式菱形. 2.scatter(x, y, marker='s', color='r')绘制散点图, ...
- PO_标准采购流程请购采购接受入库(流程)
2014-06-03 Created By BaoXinjian
- LevelDB场景分析2--Open
1.源码 1 Status DB::Open(const Options& options, const std::string& dbname, uint64_t new_ ...
- spring 多线程
http://blog.csdn.net/chszs/article/details/8219189 一.ThreadPoolTaskExecutor ThreadPoolTaskExecutor的配 ...
- mysql (已解决)Access denied for user 'root'@'localhost' (using password: NO)
找到mysql中的my.ini,在最后一行加入 skip-grant-tables 在“管理工具”-”服务” 中重启mysql 解决问题