iOS7 Xcode5 键盘的处理
来源:互联网 发布:1002服务数据出现异常 编辑:程序博客网 时间:2024/05/16 16:48
背景:
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隐藏功能分享——“字典”功能(英英字典):
在任何输入框中选中一个英文单词,此时会有选择项“复制”,“删除”...等,还有一个向右的箭头,点击这个向右的箭头后,就会出现“定义”选项,点击这个“定义”按钮即会弹出这个英语单词的英文解释。
0 0
- iOS7 Xcode5 键盘的处理
- iOS7中手势返回时键盘的处理
- iOS7时iPAD旋转的键盘需要特殊处理
- Xcode5 iOS7百度地图SDK配置 与错误处理
- IOS7.0 Xcode5 AutoLayout
- iOS7.1+Xcode5.1
- XCode5 IOS7 调试
- iOS7(Xcode5)中隐藏状态栏的方法
- iOS7(Xcode5)中隐藏状态栏的方法
- 关于Xcode5上IOS7模拟器过大的问题
- Xcode5 运行IOS7以下的,如:IOS6模拟器
- xcode5 ios7 64位 iphone5s引用第三方库报错的解决办法
- xcode5 ios7 后状态栏被导航栏遮挡的解决方案
- xcode5.0 编译ios7.0以下的sdk 方法
- iOS7(Xcode5)中隐藏状态栏的方法
- xcode5 ios7 后状态栏被导航栏遮挡的解决方案
- xcode5 ios7 64位 iphone5s引用第三方库报错的解决办法
- Xcode5.0 (ios7) 下ffmpeg的编译、导入、运行
- openwrt 透明代理上网
- linux网络协议栈(五)网络层 (11)NAT穿越
- linux网络协议栈(六)传输层 (1)传输层基本框架
- 浏览器的工作原理:新式网络浏览器幕后揭秘
- 关于AJAX的ScriptManager.RegisterClientScriptBlock无法弹出对话框的解决方法【归类】
- iOS7 Xcode5 键盘的处理
- 笑点测试中,请勿打扰
- JPA/database lock
- 卸载Oracle 11gR2 RAC
- 锂电池充放电
- jpbm表
- jQuery EasyUI Tree 增删改 编辑结点名称自动全选文本域
- Fedora10下源码编译vlc-1.1.1 流程及注意事项
- PowerDesigner修改建库脚本的方法,尤其是外键生成方式