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];
//原文:http://www.cnblogs.com/zhulin/archive/2011/10/15/2213687.html
分享到:
相关推荐
动态获取键盘高度 ios objective-c 动态获取键盘高度 ios objective-c
在我们开发中,经常用到评论等功能,在下方一个TextField,但是弹出键盘,评论框也要弹起,这时候要监听高度,本代码可完美实现此功能
系统键盘、第三方键盘完全兼容 1、键盘高度 2、键盘速度
jQuery苹果IOS虚拟键盘基于jquery.1.11.3.min.js制作,有全功能键盘、数字键盘两种形式。
获取到键盘的view,对其高度自定义,完全按照自己的需求进行布局
根据文本内容,设定字体大小,文本宽度,动态获取键盘高度!
一、效果图 二、代码 ViewController.h #import @interface ViewController : UIViewController @end ViewController.m ...#import ViewController.h #define W [UIScreen mainScreen].bounds.size.width ...
在IOS12 上,微信版本 v6.7.4 及以上,输入框获取焦点,键盘弹起,页面(webview)整体往上滚动,当键盘收起后,不回到原位,导致键盘原来所在位置是空白的。 在 IOS 上,使用第三方输入法,高度计算存在偏差,...
在 h5 项目中,我们会经常遇到一些表单页面,在输入框获取焦点时,会自动触发键盘弹起,而键盘弹出在 IOS 与 Android 的 webview 中表现并非一致,同时当我们主动触发键盘收起时也同样存在差异化。 键盘弹出 IOS:...
用键盘通知获取键盘的高度,动画时长,动画效果等来设定输入框的状态
本文介绍了React-Native键盘遮挡问题,分享给大家 在开发中经常遇到需要输入的地方,RN给我们提过的...关于原生iOS获取键盘高度我就不多说了,网上一大堆,我直接贴上我的代码,自己根据RN写的原生模块: // // Keyboa
状态栏20键盘高度216导航44 最少2位 补0 // UIColor *color2 = [[UIColor alloc] initWithRed:0 green:1 blue:0 alpha:1]; // button setTitle:@"点我吧" forState:UIControlStateNormal]; // [button addTarget:...
keyboard_visibility 通知服务,用于软键盘可见性 用法 将依赖项添加到项目的... 导入package:keyboard_visibility/keyboard_visibility.dart ,实例化KeyboardVisibilityNotification并使用Android和iOS通知获取有关
封装类网易新闻评论新闻输入框,监听键盘改变显示高度,自带黑灰背景 用法: step1:加到你需要的vc中 _textView =[QZTopTextView topTextView]; _textView.delegate = self; [self.view addSubview:_...
该软件包添加了一个视图扩展,该扩展将iOS软件键盘观察器添加到环境中。 更多文档和更新即将发布。 这是一个很早的草案。 感谢您的任何反馈或贡献! import SwiftUI import SwiftUI_Keyboard_Observer @main ...
据字符串的多少,动态调整UILabel的宽度或高度ios 将字符串转换为日期时间格式ios获取时间和星期(注意week1是星期天)隐藏状态栏屏幕变动检测设置视图背景图片键盘覆盖输入框将plist文件中的数据赋给数组手指的触摸从...
退回输入键盘 2 ...获取IOS设备的基本信息 42 用NSDateFormatter调整时间格式的代码 42 UIView设置成圆角方法 43 iPhone里的frame和bounds区别 43 Objective-C内存管理 44 iphone更改键盘右下角按键的type 45
数字证书:从文件中读取数字证书,生成文件输入流,输入文件为c:/mycert.cer,获取一个处理X.509证书的证书工厂…… Java+ajax写的登录实例 1个目标文件 内容索引:Java源码,初学实例,ajax,登录 一个Java+ajax写...
数字证书:从文件中读取数字证书,生成文件输入流,输入文件为c:/mycert.cer,获取一个处理X.509证书的证书工厂…… Java+ajax写的登录实例 1个目标文件 内容索引:Java源码,初学实例,ajax,登录 一个Java+ajax写...