消息机制之通知中心传值(NSNotificationCenter)中详细说了用户自定义的通知中心传值. 其实在系统中还有很多系统自带的通知, 系统来控制 post 时机, 如键盘的相关通知, UIWindow 的相关通知等等. 在这些通知中, 系统在 post 时, 自带了UserInfo, 今天来说一说键盘的通知和其 UserInfo.

例举关于 UIWindow和 Keyboard的通知:

UIWindowDidBecomeVisibleNotification; // nil
UIWindowDidBecomeHiddenNotification;  // nil
UIWindowDidBecomeKeyNotification;     // nil
UIWindowDidResignKeyNotification;     // nilUIKeyboardWillShowNotification; //键盘将要出现
UIKeyboardDidShowNotification;  //键盘已经出现
UIKeyboardWillHideNotification;  //键盘将要消失
UIKeyboardDidHideNotification;  //键盘已经消失
UIKeyboardWillChangeFrameNotification; //键盘将要改变 frame
UIKeyboardDidChangeFrameNotification; //键盘已经改变 frame
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

键盘的 UserInfo

keyboard 的通知的 UserInfo中包含的内容有如下:

UIKeyboardFrameBeginUserInfoKey //初始的 frame
UIKeyboardFrameEndUserInfoKey   //结束的 frame
UIKeyboardAnimationDurationUserInfoKey  //持续的时间
UIKeyboardAnimationCurveUserInfoKey  //UIViewAnimationCurve
UIKeyboardIsLocalUserInfoKey  //是否是当前 App的键盘//已弃用
UIKeyboardCenterBeginUserInfoKey   NS_DEPRECATED_IOS
UIKeyboardCenterEndUserInfoKey     NS_DEPRECATED_IOS
UIKeyboardBoundsUserInfoKey        NS_DEPRECATED_IOS     
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

例子

- (void)viewDidLoad {[super viewDidLoad];//监听,当键盘出现时自动发送消息[[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 *)aNotification
{NSDictionary *userInfo = [aNotification userInfo];//获取键盘初始的frameNSValue *beginFrame = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];CGRect keyboardBeginRect = [beginFrame CGRectValue];//获取键盘结束的frameNSValue *endFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];CGRect keyboardEndRect = [endFrame CGRectValue];//获取当前键盘高度CGFloat keyboardHeight = keyboardEndRect.size.height;//获取持续的时间NSNumber *time = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//是否是当前 app 的键盘NSNumber *isCurrentKB = [userInfo objectForKey:UIKeyboardIsLocalUserInfoKey];}
//键盘消失时接受消息通知
- (void)keyboardWillHide:(NSNotification *)aNotification
{}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

打印结果

打印 KeyBoard 的 notification, 后三个在 iOS3.2后已被弃用. 

name = UIKeyboardWillShowNotification;  
userInfo = { 
UIKeyboardAnimationCurveUserInfoKey = 7; 
UIKeyboardAnimationDurationUserInfoKey = “0.25”’ 
UIKeyboardFrameBeginUserInfoKey = “NSRect: {{0, 667}, {375, 258}}”; 
UIKeyboardFrameEndUserInfoKey = “NSRect: {{0, 409}, {375, 258}}”; 
UIKeyboardIsLocalUserInfoKey = 1; 
UIKeyboardBoundsUserInfoKey = “NSRect: {{0, 0}, {375, 258}}”; 
UIKeyboardCenterBeginUserInfoKey = “NSPoint: {187.5, 796}”; 
UIKeyboardCenterEndUserInfoKey = “NSPoint: {187.5, 538}”; 

}

获取键盘通知的 UserInfo相关推荐

  1. ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)

    背景: ios5之前,iphone上的键盘的高度是固定为216.0px高的,中文汉字的选择框是悬浮的,所以不少应用都将此高度来标注键盘的高度(包括米聊也是这么做的). 可是在ios5中,键盘布局变了, ...

  2. iOS键盘监听以及获取键盘高度

    在文本输入时,界面会弹出键盘.有时,当文本输入框过低,被键盘遮挡,使用户无法看见输入框文本内容,这就使得用户体验过低. 所以需要我们对键盘进行监控并获取键盘高度,调节界面或文本框高度进行处理.如下图, ...

  3. 猫猫学IOS(十八)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复

    猫猫分享,必须精品 素材代码地址:http://blog.csdn.net/u013357243/article/details/45000699 原文地址:http://blog.csdn.net/ ...

  4. (素材源码)猫猫学IOS(十八)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复

    猫猫分享,必须精品 素材代码地址:http://download.csdn.net/detail/u013357243/8585703 原文地址:http://blog.csdn.net/u01335 ...

  5. iOS【UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复】

    先看图片 第一步完成tableView和Cell的架子的图  完善图片  键盘弹出设置后图片:  自动回复图:  粗狂的架子 tableView和Cell的创建 首相tableView为了学习方便就直 ...

  6. ios键盘通知和自定义键盘

    一.键盘通知 当文本View(如UITextField,UITextView, UIWebView内的输入框)进入编辑模式成为first responder时,系统会自动显示键盘.成为firstres ...

  7. iPhone键盘通知与键盘定制

    一.键盘通知 当文本View(如UITextField,UITextView, UIWebView内的输入框)进入编辑模式成为first responder时,系统会自动显示键盘.成为firstres ...

  8. iOS获取键盘的高度

    - (void)viewDidLoad { [super viewDidLoad]; //增加监听,当键盘出现或改变时收出消息 [[NSNotificationCenter defaultCenter ...

  9. UIDevice通知,键盘通知

    •UIDevice类提供了一个单粒对象,它代表着设备,通过它可以获得一些设备相关的信息,比如电池电量值(batteryLevel).电池状态(batteryState).设备的类型(model,比如i ...

最新文章

  1. sap 一代增强_SAP增强Enhancement
  2. jquery插件编写
  3. intent几种传值数组、对象、集合(Array,Object,List)
  4. 算法训练 和为T 深度搜索
  5. 2021游戏市场风向标报告
  6. 虚拟机如何连接服务器系统,Horizon 连接服务器最大连接数和虚拟机配置
  7. 转载一下如何联系口语
  8. 【人脸识别】基于matlab GUI PCA+SVM人脸识别(准确率)【含Matlab源码 823期】
  9. oracle练习之查询1(where等)
  10. 基于STM32设计的数字电子秤
  11. 监测-病毒篇(病毒的了解和认识)
  12. blockUI弹出层
  13. 2021华为秋招算法工程师面试经历(实习过)
  14. 苦难是人生中必须经历的一课
  15. 逻辑地址和物理地址的相互转换
  16. SpringBoot整合log4j2
  17. flash.js 劫持怎么解决
  18. 微盟股价快速飞升的背后:WOS系统将驱动长效增长
  19. k8s镜像一直failed pull images
  20. 小米路由器linux界面,小米路由器配置ssh登录方法 | 192.168.1.1登陆页面

热门文章

  1. 转行IC的人都会问到的五个问题
  2. 山石网科Hillstone防火墙VLAN接口配置方案(官新版)
  3. 什么是用户体验五要素
  4. JAVA知识库系统计算机毕业设计Mybatis+系统+数据库+调试部署
  5. AutoCAD中我们如何进行创建和设置管理图层?
  6. Go语言读取解析yml文件,快速转换yml到go struct
  7. 每周一品 · 海尔贝克阵列 Halbach Array
  8. RedHat4找不到网卡和mysql启动不了解决方案
  9. 小孩刚烫伤之后留下的疤痕一般多久开始增生
  10. 日本大数据应用环境和发展状况