『IOS』iPhone和ipad键盘高度及键盘响应事件
原文地址:http://blog.csdn.net/benbenxiongyuan/article/details/7892019
IPAD键盘高度:
portrait 264
landscape 352.
iPhone键盘高度:
Portrait 216
Landscape 140
背景:
ios5之前,iphone上的键盘的高度是固定为216.0px高的,中文汉字的选择框是悬浮的,所以不少应用都将此高度来标注键盘的高度。
可是在ios5中,键盘布局变了,尤其是中文输入时,中文汉字选择框就固定在键盘上方,这样就使得原本与键盘紧密贴合的界面视图被中文汉字选择框给覆盖住了。一方面影响了界面的美观,另一方面,如果被覆盖的部分就是文本输入框的话,用户就无法看到输入的内容了。因此这个问题就必须得解决了。
解决方法:
其实在一开始使用216.0px这个固定值来标注键盘的高度就是错误的。因为在ios3.2以后的系统中,苹果就提供了键盘使用的api以及demo程序——“KeyboardAccessory”。
处理键盘事件的正确方法是这样的:(包括获取键盘的高度以及键盘弹出和消失动画的时间)
1)在要使用键盘的视图控制器中,接收键盘事件的通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; // 键盘高度变化通知,ios5.0新增的 #ifdef __IPHONE_5_0 float version = [[[UIDevice currentDevice] systemVersion] floatValue]; if (version >= 5.0) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil]; }#endif
2)然后添加键盘事件的处理代码:
获取到当前keyboard的高度以及动画时间,然后对视图进行对应的操作即可。
#pragma mark -#pragma mark Responding to keyboard events- (void)keyboardWillShow:(NSNotification *)notification { /* Reduce the size of the text view so that it's not obscured by the keyboard. Animate the resize so that it's in sync with the appearance of the keyboard. */ NSDictionary *userInfo = [notification userInfo]; // Get the origin of the keyboard when it's displayed. NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position. CGRect keyboardRect = [aValue CGRectValue]; // Get the duration of the animation. NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval animationDuration; [animationDurationValue getValue:&animationDuration]; // Animate the resize of the text view's frame in sync with the keyboard's appearance. [self moveInputBarWithKeyboardHeight:keyboardRect.size.height withDuration:animationDuration];} - (void)keyboardWillHide:(NSNotification *)notification { NSDictionary* userInfo = [notification userInfo]; /* Restore the size of the text view (fill self's view). Animate the resize so that it's in sync with the disappearance of the keyboard. */ NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval animationDuration; [animationDurationValue getValue:&animationDuration]; [self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];}
3)在视图控制器消除时,移除键盘事件的通知:
[[NSNotificationCenter defaultCenter] removeObserver:self];
ps:
ios5隐藏功能分享——“字典”功能(英英字典):
在任何输入框中选中一个英文单词,此时会有选择项“复制”,“删除”...等,还有一个向右的箭头,点击这个向右的箭头后,就会出现“定义”选项,点击这个“定义”按钮即会弹出这个英语单词的英文解释。
动态获取keyboard大小:
- (void)viewDidLoad
{[super viewDidLoad];//增加监听,当键盘出现或改变时收出消息[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];//增加监听,当键退出时收出消息[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];}//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{//获取键盘的高度NSDictionary *userInfo = [aNotification userInfo];NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];CGRect keyboardRect = [aValue CGRectValue];int height = keyboardRect.size.height;
}//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{}
动态调整输入框的位置:
实现方法:
1)将输入框的代理设置为self
(在lb文件中将输入框的delegate设置为File’s Owner 。或者使用代码textField.delegate = self;
2)将输入框所对应的ViewController.h设置实现了UITextFieldDelegate协议
在ViewController.m文件中实现UITextFieldDelegate的三个方法即可:
/开始编辑输入框的时候,软键盘出现,执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{ CGRect frame = textField.frame; int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216 NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示 if(offset > 0) self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height); [UIView commitAnimations];
} //当用户按下return键或者按回车键,keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{ [textField resignFirstResponder]; return YES;
} //输入框编辑完成以后,将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{ self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
该方法不适用于,当前页面多个输入框的情况,需要修改
『IOS』iPhone和ipad键盘高度及键盘响应事件相关推荐
- iPhone和ipad键盘高度及键盘响应事件 摘
iPhone和ipad键盘高度及键盘响应事件 http://blog.csdn.net/benbenxiongyuan/article/details/7892019 IPAD键盘高度: portra ...
- iPhone和ipad键盘高度及键盘响应事件
IPAD键盘高度: portrait 264 landscape 352. iPhone键盘高度: Portrait 216 Landscape 140 背景: ios5之前,iphone上的 ...
- ios 区分iphone ipod ipad的方法及获取设备名称。
coding如何区分iphone ipod & ipad 的几种方法 UI_USER_INTERFACE_IDIOM Returns the interface idiom supported ...
- h5如何动态获取键盘高度_H5 键盘兼容性小结
在 H5 项目中,我们会经常遇到页面中存在单个甚至多个 input/textarea 输入框与底部固定元素的布局情况.在 input/textarea 输入框获取焦点时,会自动触发键盘弹起,而键盘弹出 ...
- ios 判断iPhone、iPad硬件型号
方法1: #include <sys/sysctl.h> - (NSString *) platform { size_t size; sysctlbyname(& ...
- 如何将音视频发布到移动设备,android,ios,iphone,ipad等
在现实生活中,手机和移动设备使用地越来越多,如何将音视频点播和直播发布到移动设备,这就涉及到很多相关的知识,比如android接收的音视频有哪些格式,分辨率是什么样子的,而ios设备接收的情况又是什么 ...
- ipad和iphone适配_如何更改您的iPhone和iPad键盘的语言
ipad和iphone适配 Khamosh Pathak Khamosh Pathak Your iPhone or iPad usually comes pre-loaded with your k ...
- 如何在iPhone和iPad上的Safari中在网页上查找文本
Sometimes it's tough to find specific information within a web page in Safari for iPhone or iPad. Lu ...
- ipad iphone开发_如何修复iPhone或iPad上崩溃的应用程序
ipad iphone开发 N.Z.Photography/ShutterstockNZ摄影/快门 Apps can crash or freeze on iPhones and iPads, jus ...
最新文章
- 面试官:哥们,你们的系统架构中为什么要引入消息中间件?
- 中国有机玻璃市场竞争策略与投资前景建议报告2022-2028年
- 信息系统项目管理师:第9章:项目人力资源管理-章节重点
- 在C4C UI里嵌入CRM WebClient UI
- 根据自己的博客数据统计国内IT人群
- sql 命令使用简单记录
- CTF-Bugku逆向题Android方法归纳
- iOS VideoToolBox decoder解码失败(-12909和-12911)问题解决
- 笔记︱多种常见聚类模型以及分群质量评估(聚类注意事项、使用技巧)
- Android设计模式(1)----单例模式
- 【python与数据分析】Python与数据分析概述
- npm的那些“坑”——持续更新
- python能参加奥赛吗-家长分享孩子学习NOIP信息学奥赛的经历
- php模板开发教程,discuz模板开发教程系列教程整理
- 【学习日记2023.4.25】之 前后端分离_前端工程化_Vue组件库Element_Vue路由_打包部署
- 开学啦!来淘宝教育体验开学第一课
- 老挑毛u盘一键装系统计算机意外地,u盘装系统 重装Win7系统出现提示计算机意外的重新启动或遇到错误怎么处理 我已经删除了所有分...
- Dom获取 属性操作
- 有考c语言的软件工程专硕吗,2020年南开大学软件工程硕士考研真题试卷及试题答案,C语言与数据结构考研试题下载...
- harbor安装时出现ERROR: An HTTP request took too long to complete. Retry with --verbose to obtain debug in
热门文章
- 管理员工绩效的 7 个最佳策略
- html 导航栏 选中状态,CSS导航菜单高亮选中菜单项
- 制作一个小工具:自动生成“将特定枚举值转换成字符串的C++函数”的代码
- 梳状滤波器CIC整理
- leetcode 45. 跳跃游戏 II 46. 全排列
- php添加网站ico图标,如何添加网站ICO图标,添加ico图标的方法
- 邦彦技术上市首日跌15%:公司市值37亿 劲牌是二股东
- hd2 - 如何刷wm6.5
- (附源码)计算机毕业设计SSM欢迎智能停车场管理系统
- 一点通发票打印软件 v1.2 下载