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:文件 ...
随机推荐
- automake安装出错
automake命令出错 configure.ac:64: error: possibly undefined macro: AM_ICONV If this token and others ...
- ajax操作登录
js文件中的内容(ajax.operate.js) ;(function ($, window) { var _ajaxOperate = window.ajaxOperate || {}; _aja ...
- PLSQL_统计信息系列06_统计信息的历史和日志
20150506 Created By BaoXinjian
- Android 布局之LinearLayout 子控件weight权重的作用详析
关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法: 说法一:值越大,重要性越高,所占用的空间越大: 说法 ...
- Linux系统休眠和设备中断处理
一.设备IRQ的suspend和resume 本小节主要解决这样一个问题:在系统休眠过程中,如何suspend设备中断(IRQ)?在从休眠中唤醒的过程中,如何resume设备IRQ? 一般而言,在系统 ...
- 透析Java本质-谁创建了对象,this是什么
是构造方法创建的对象吗 package com.java.essence_36; import java.util.ArrayList; import java.util.List; /** * Cr ...
- PixelLink
简介 论文题目:PixelLink: Detecting Scene Text via Instance Segmentation 论文地址:https://arxiv.org/abs/1801.01 ...
- intellij IDEA开发node.js
现在网上好像关于IDEA开发node.js的讲解不是太多,今天试了一下,竟然成功了.... 1.安装nodejs http://nodejs.org/download/ 自动根据系统下载自己的版本n ...
- Windows上怎么安装ELK
In this guide I will show that it is also possible to run Logstash on a Windows Server 2012 machine ...
- OpenGl学习 glenable()函数理解
glEnable用于启用各种功能.功能由参数决定.与glDisable相对应.glDisable是用来关闭的.两个函数参数取值是一至的. 参数说明:void glEnable(GLenum cap)G ...