一、发送通知

 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边练边学--通知机制和键盘处理小练习的更多相关文章

  1. iOS边练边学--通知机制和键盘处理

    一.通知中心(NSNotificationCenter) 每一个程序都有一个通知中心实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以想通知中心发布通知(NSNotification),描述 ...

  2. iOS边练边学--Http网络再学习,简单介绍

    一.URL 什么是URL URL中常见的协议 二.Http Http的基本通信过程 发送Http请求的方法 GET 和 POST 对比 GET 和 POST 的选择 三.iOS中的Http学习 iOS ...

  3. iOS边练边学--多线程介绍、NSThread的简单实用、线程安全以及线程之间的通信

    一.iOS中的多线程 多线程的原理(之前多线程这块没好好学,之前对多线程的理解也是错误的,这里更正,好好学习这块) iOS中多线程的实现方案有以下几种 二.NSThread线程类的简单实用(直接上代码 ...

  4. iOS边练边学--CALayer,非根层隐式动画,钟表练习

    一.CALayer UIView之所以能显示在屏幕上,完全是因为他内部的一个图层 在创建UIView对象时,UIView内部会自动创建一个图层(即CALayer对象),通过UIView的layer属性 ...

  5. iOS边练边学--UIGestureRecognizer手势识别器简单介绍

    iOS 3.2之后,苹果退出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度. 一.UIGestureRecognizer UIGestureRe ...

  6. iOS边练边学--触摸事件以及能够拖拽的UIView的练习

    一.用户在使用APP的过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: 二.响应者对象 在iOS中只有继承了了UIResponder的对象才能接受并处理事件,这样的对象称之为“响应者对象 ...

  7. iOS边练边学--应用数据存储的常用方式(plist,Preference,NSKeyedArchiver)其中的三种

    iOS应用数据存储的常用方式: XML属性列表(plist)归档 Preference(偏好设置) NSKeyedArchiver归档(NSCoding) SQLite3--这里暂且不讲 Core D ...

  8. iOS边练边学--图片的拉伸

    图片拉伸方法一: IOS 5.0以后才有的方法: - (void)viewDidLoad { [super viewDidLoad]; UIImageView *imageView = [[UIIma ...

  9. iOS边练边学--NSURLSessionDataTask实现文件真正的断点续传

    实现重点: NSURLSessionDataTask要设置请求头,从路径中获取文件已经下载的长度(文件没有下载过的话,长度为0).通过这个长度设置请求的Range 如图: 接收到请求的时候key:文件 ...

随机推荐

  1. SSL原理

    http://blog.csdn.net/terryzero/article/details/5921791SSL的原理以前一直很模糊,看了下面这篇文章后清楚了许多,为了方便以后的回顾,所以转载下 R ...

  2. Linux命令-服务管理命令:chkconfig

    chkconfig --list 查看服务自启动状态列表,等同于查看服务列表 设置某一个服务为自启动服务: chkconfig 服务名 on 修改服务的启动级别为3,,5 查看某一个服务时候已经运行了 ...

  3. 跳出框架iframe的操作语句

    常用的iframe操作语句 ①   本页面跳转语句: "window.location.href" 或者 "location.href" ②   上一层页面跳转 ...

  4. mySQL把秒转换成日期

    mysql> SELECT SEC_TO_TIME (3600); +--------------------+ | SEC_TO_TIME (3600) | +---------------- ...

  5. Opening Default document on IIS (HTML With WebAPI)

    Question: I've a deployed ASP.NET Web API with a website on the same folder that consume it. When I ...

  6. Android开发2——创建测试项目

    一.创建普通Android项目   二.在AndroidManifest.xml添加两个配置 <?xml version="1.0" encoding="utf-8 ...

  7. Linux下hosts、host.conf、resolv.conf的区别

    /etc/resolv.conf 该文件是DNS域名解析的配置文件,它的格式很简单,每行以一个关键字开头,后接配置参数.resolv.conf的关键字主要有四个,分别是:nameserver   #定 ...

  8. RednaxelaFX写的文章/回答的导航帖

    https://www.zhihu.com/people/rednaxelafx/answers http://hllvm.group.iteye.com/group/topic/44381#post ...

  9. nyoj 504 课程设计

    课程设计 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 新学期伊始,Gangster 老师又在为如何给学生分配课程设计题目而犯愁,Gangster老师老共有 N 名学生 ...

  10. DirectStream、Stream的区别-SparkStreaming源码分析02

    转http://hadoop1989.com/2016/03/15/KafkaStreaming/ 在Spark1.3之前,默认的Spark接收Kafka数据的方式是基于Receiver的,在这之后的 ...