如何完美适应iOS中的ios 键盘高度变化化

iOS(401)
很久以前写了一篇文章,讨论如何《》,今天觉得可以完美跟随:
通过获取键盘消息的开始状态、结束状态,以及变化周期,可以计算出具体的Y偏移,从而在相同时间里做相同偏移量。
记住在使用
&&[self.navigationController
popViewControllerAnimated:YES];之前一定要设置
self.contentTextField.text=@&&;
否则下次偏移有可能出错。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1629199次
积分:15739
积分:15739
排名:第548名
原创:55篇
转载:880篇
评论:85条
(1)(1)(1)(15)(21)(4)(6)(42)(4)(1)(5)(2)(11)(18)(21)(43)(3)(4)(11)(5)(3)(2)(7)(2)(4)(39)(60)(24)(86)(118)(92)(2)(2)(2)(1)(5)(18)(3)(17)(20)(97)(59)(35)(20)(1)在ios开发中,键盘很常用。在sdk版本5.0以前,键盘高度是固定值216px;5.0出来以后,键盘高度会随着键盘语言变化(中文要高些),在这种情况下一般而言对于界面需要重新布局。方法是利用NSNotificationCenter。
UIKeyboardWillShowN
UIKeyboardDidShowN
UIKeyboardWillHideN
UIKeyboardDidHideN
这几个notification是5.0sdk之前就有的,顾名思义就知道意思了。
UIKeyboardWillChangeFrameNotification
__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
UIKeyboardDidChangeFrameNotification
__OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
这两个是sdk 5.0以后出来的,用来处理键盘高度的变化。
使用方法是:首先在notification注册观察者,比如:
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
当键盘高度将要变化时,就会收到通知,在通知的参数中可以得到键盘目前的高度和变化的目标高度,比如:
-(void)keyboardWillChangeFrame:(NSNotification*)notif{
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2
NSValue *keyboardBoundsValue = [[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
NSValue *keyboardBoundsValue = [[notif userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];
CGRect keyboardEndRect = [keyboardBoundsValue CGRectValue];
CGRect inputFrame = self.feedBackTextView.
//kb 216 vs textFrame 185
float delta = keyboardEndRect.size.height - 216;
float originalHeight = inputFrame.size.
inputFrame.size.height = 185 -
if (inputFrame.size.height != originalHeight) {
self.feedBackTextView.frame = inputF
self.feedBackBackgroundView.frame = inputF
另外一些从notification.userInfo中可以取得的key如下:
UIKeyboardFrameBeginUserInfoKey
// NSValue of CGRect
UIKeyboardFrameEndUserInfoKey
// NSValue of CGRect
UIKeyboardAnimationDurationUserInfoKey // NSNumber of double
UIKeyboardAnimationCurveUserInfoKey
// NSNumber of double
notif中userInfo的完整信息如下:
keyboardChange:{
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 372}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 588}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
UIKeyboardFrameChangedByUserInteraction = 0;
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
下面是一个完整的解决方案,用户需要知道键盘高度的细致变化
#pragma mark Keyboard
-(void)keyboardWillChangeFrame:(NSNotification*)notif{
NSLog(@"keyboardChange:%@",[notif userInfo]);
float keyboadHeightBegin = 0;
float keyboadHeightEnd = 0;
float animationDuration = [[[notif userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
UIViewAnimationCurve animationCurve = [[[notif userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2
CGRect keyboardBeginFrame = [[[notif userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect keyboardEndFrame = [[[notif userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboadHeightBegin = 480 - keyboardBeginFrame.origin.y;
keyboadHeightEnd = 480 - keyboardEndFrame.origin.y;
//these deprecated after iOS 3.2
CGRect keyboardBounds = [[[notif userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
CGPoint keybordCenterBegin = [[[notif userInfo] objectForKey:UIKeyboardCenterBeginUserInfoKey] CGPointValue];
CGPoint keybordCenterEnd = [[[notif userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue];
keyboadHeightBegin = 480 - (keybordCenterBegin.y - keyboardBounds.size.height / 2);
keyboadHeightEnd = 480 - (keybordCenterEnd.y - keyboardBounds.size.height / 2);
NSLog(@"keyboardHeightChangeFrom:%.2f,To:%.2f",keyboadHeightBegin,keyboadHeightEnd);
if (keyboadHeightEnd > 0) {
//keyboard show or change frame
[UIView animateWithDuration:animationDuration delay:0 options:animationCurve animations:^{
} completion:^(BOOL finished) {
//keyboard hide
-(void)keyboardDidChangeFrame:(NSNotification*)notif{
//info like willChangeFrame
-(void)keyboardWillShow:(NSNotification*)notif{
//keyboard height will be 216, on iOS version older than 5.0
[UIView animateWithDuration:0.3f animations:^{
self.contentTableView.height = 480 - 44 - 216;
-(void)keyboardWillHide:(NSNotification*)notif{
[UIView animateWithDuration:0.3f animations:^{
self.contentTableView.height = 480 - 44 - 28;
-(void)registerKeyboardEvent{
float systemVer = [[[UIDevice currentDevice] systemVersion] floatValue];
if(systemVer >= 5.0) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
-(void)unregisterKeyboardEvent{
if([[[UIDevice currentDevice] systemVersion] floatValue] > 5.0) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
下面这个解决方案就只考虑键盘出现和消失的处理
#pragma mark Keyboard
-(void)keyboardWillShow:(NSNotification*)notif{
//keyboard height will be 216, on iOS version older than 5.0
CGRect boxFrame = self.loginBoxView.
boxFrame.origin.y = 50;
[UIView animateWithDuration:0.3f animations:^{
self.loginBoxView.frame = boxF
-(void)keyboardWillHide:(NSNotification*)notif{
CGRect boxFrame = self.loginBoxView.
boxFrame.origin.y = 216;
[UIView animateWithDuration:0.3f animations:^{
self.loginBoxView.frame = boxF
//在viewdidload中调用
-(void)registerKeyboardEvent{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//在viewdidunload中调用
-(void)unregisterKeyboardEvent{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
***text***
* Listitem
华大师的主页
&copy uniEagle问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
iOS中,键盘的高度是不可变的吗?请问有什么办法可以改变它的高度吗?
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
你应该改变自己的布局,而不是改变别人的键盘高度。
上升到人生哲理就是:
改变你能改变的,接受你改变不了的。:P
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
基本上是固定的,输入法不一样键盘高度会有点出入,不能手动改变键盘的高度的,除非你自己写一个键盘
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
直接按安卓
我是小尾巴小尾巴
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
第三方键盘是可以调整高度的,比如:搜狗输入法、百度输入法等。
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
自定义键盘就可以改变
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
可以创建一个附件视图-如果想要加按钮的话,键盘高度的不会 //创建附件视图
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
//打开图片视图的可交互性
imageView.userInteractionEnabled = YES;
imageView.image = [UIImage imageNamed:@"inputImage"];
_passField.inputAccessoryView = imageV
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
首先,别人的键盘高度无法改变,但他不是不可变的,怎么说呢,例如系统默认键盘,你切换中英文的时键盘的高度是会变化的,但是这个变化不受你代码控制。反而你要根据他的变化调整你的UI,例如QQ的聊天界面,输入框总是停留在键盘上面,所以键盘高度变化后你得layout一下。
简单的办法是接收下列通知,进行所需的相应操作
UIKIT_EXTERN NSString *const UIKeyboardWillShowN
UIKIT_EXTERN NSString *const UIKeyboardDidShowN
UIKIT_EXTERN NSString *const UIKeyboardWillHideN
UIKIT_EXTERN NSString *const UIKeyboardDidHideN
不过这种轮子已经,例如
然后说说自定义键盘(inputView)这个高度就是可以改变的,不过代价是你得手写一个键盘,还是参考QQ输入表情的键盘应该就是自定义的。
希望对你有帮助
为工程师推荐兼职,详情点我头像
同步到新浪微博
分享到微博?
你好!看起来你挺喜欢这个内容,但是你还没有注册帐号。 当你创建了帐号,我们能准确地追踪你关注的问题,在有新答案或内容的时候收到网页和邮件通知。还能直接向作者咨询更多细节。如果上面的内容有帮助,记得点赞 (????)? 表示感谢。
明天提醒我
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
扫扫下载 App
SegmentFault
一起探索更多未知自适应iOS的不同键盘高度_Linux编程_Linux公社-Linux系统门户网站
你好,游客
自适应iOS的不同键盘高度
来源:Linux社区&
作者:JasonLee
在iOS 5中,键盘的高度是会变化的,比如切换到中文输入法时会在键盘上方多出一层候选字区域,如下图:
而在英文输入法下是没有文字候选区域的。
因此在用户输入场景下,布局的美观和可用性可能受到键盘高度变化的影响,因此需要动态适应键盘高度。
解决方案是监听键盘呼出事件的消息:
[[NSNotificationCenter&defaultCenter]&addObserver:self&selector:@selector(keyboardWillShow:)&name:UIKeyboardWillShowNotification&object:nil];&&
针对键盘高度做出自适应:
-&(void)keyboardWillShow:(NSNotification&*)notification&&
&&&&static&CGFloat&normalKeyboardHeight&=&216.0f;&&
&&&&NSDictionary&*info&=&[notification&userInfo];&&
&&&&CGSize&kbSize&=&[[info&objectForKey:UIKeyboardFrameEndUserInfoKey]&CGRectValue].&&
&&&&CGFloat&distanceToMove&=&kbSize.height&-&normalKeyboardH&&
最后,移除观察者。
相关资讯 & & &
& (10/22/:10)
& (05/27/:52)
& (03/10/:45)
& (06/14/:25)
& (03/13/:23)
& (02/10/:10)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款//在遇到有输入的情况下。由于现在键盘的高度是动态变化的。中文输入与英文输入时高度不同。所以输入框的位置也要做出相应的变化
#pragma mark - keyboardHight
-(void)viewWillAppear:(BOOL)animated
[self registerForKeyboardNotifications];
-(void)viewWillDisappear:(BOOL)animated
[[NSNotificationCenter defaultCenter] removeObserver:self];
- (void)registerForKeyboardNotifications
//使用NSNotificationCenter 鍵盤出現時
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
//使用NSNotificationCenter 鍵盤隐藏時
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
//实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
- (void)keyboardWasShown:(NSNotification*)aNotification
NSDictionary* info = [aNotification userInfo];
//kbSize即為鍵盤尺寸 (有width, height)
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].//得到鍵盤的高度
NSLog(@"hight_hitht:%f",kbSize.height);
if(kbSize.height == 216)
keyboardhight = 0;
keyboardhight = 36;
//252 - 216 系统键盘的两个不同高度
//输入框位置动画加载
[self begainMoveUpAnimation:keyboardhight];
//当键盘隐藏的时候
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
//do something
//(TextView) 当键盘开始输入前。时行计算与动画加载
-(void)textViewDidBeginEditing:(UITextView *)textView
NSLog(@"gegin animation");
sendMsgTextView =textV
resultCommunityTableview.frame = CGRectMake(0, 36, 320, 150);
//动画加载
[self begainMoveUpAnimation:0.0 ];
//关闭键盘(TextView) 换行时。隐藏键盘
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
resultCommunityTableview.frame = CGRectMake(0, 36, 320, 376);
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
return YES;
//输入结束时调用动画(把按键。背景。输入框都移下去)
-(void)textViewDidEndEditing:(UITextView *)textView
NSLog(@"tabtabtab");
[self endEditAnimation];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
//判断当前输入法
-(void)textViewDidChangeSelection:(UITextView *)textView
NSLog(@"wewe:%@",[[UITextInputMode currentInputMode] primaryLanguage]);
if ([[UITextInputMode currentInputMode] primaryLanguage] == @"en-US") {
NSLog(@"en-US");
NSLog(@"zh-hans");
阅读(...) 评论()

我要回帖

更多关于 textarea高度自适应 的文章

 

随机推荐