注意:要想实时获取键盘的高度,比如当前如果是中文那么就会增高的。那么需要使用  UIKeyboardFrameEndUserInfoKey 而不是 UIKeyboardFrameBeginUserInfoKey,后者将获取到的是上次的高度,而前者是键盘动画完成后的最终高度。

The following code (sorry for the length) displays an odd behavior under iOS 4.3 (maybe others version too). In this example, there are three UITextFields that have three different sized keyboards. If you start editing one text field and then touch "return" dismissing the keyboard, each time the keyboard size is returned correctly in UIKeyboardWillShowNotification and UIKeyboardDidShowNotification using UIKeyboardFrameBeginUserInfoKey.

see below:

- (void) keyboardWillShowNotification:(NSNotification *)aNotification

and

- (void) keyboardDidShowNotification:(NSNotification *)aNotification

Note that this is the expected behavior.

action                 reported keyboard size  expected keyboard size
-----------------------------------------------------------------
touch one &return100100
touch two &return200200
touch normal &return216216
n &return keyboard size(n) keyboard size(n)

The unexpected behavior is if you start editing a text field the size of the first keyboard is reported (expected). When you touch the second text field (without touching return), the size of first keyboard is reported (unexpected) instead of the size of the second. When you touch the third text field (without touching return), the size of the second keyboard size is reported (unexpected) instead of the size of the third. For the second to nth times, it seems like it is reporting the previous keyboard's size not the one that will be displayed.

action        reported keyboard size  expected keyboard size
--------------------------------------------------------
touch one 100100
touch two 100200
touch normal 200216
touch one 216100
n keyboard size(n-1) keyboard size(n)

Before I send in a bug report, I just want to make sure that I have not over looked anything.

FYI I stubbled upon this while trying to do the right thing (using UIKeyboardWillShowNotification or UIKeyboardDidShowNotification and UIKeyboardFrameBeginUserInfoKey to get the keyboard's size) when shifting a view so that a text field that would have been obscured by a keyboard is visible. Reference:

How to make a UITextField move up when keyboard is present

iOS Library: Text, Web, and Editing Programming Guide for iOS --> Managing the Keyboard

iOS Library: Scroll View Programming Guide for iOS --> Creating and Configuring Scroll Views

BugVC.h

#import <UIKit/UIKit.h>@interfaceBugVC:UIViewController<UITextFieldDelegate>{UITextField*oneTF;UITextField*twoTF;UITextField*normalTF;UILabel*keyboardWillShowNotificationL;UILabel*keyboardDidShowNotificationL;}-(void) oneReturnTouchDown:(id)sender;-(void) twoReturnTouchDown:(id)sneder;-(void) keyboardWillShowNotification:(NSNotification*)aNotification;-(void) keyboardDidShowNotification:(NSNotification*)aNotification;@end

BugVC.m

#import "BugVC.h"@implementationBugVC-(id) init
{if(!(self=[super init])){returnself;}// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =// One text field with 100 height keyboard
oneTF =[[UITextField alloc] initWithFrame:CGRectMake(10,10,300,30)];
oneTF.borderStyle =UITextBorderStyleRoundedRect;
oneTF.text =@"100";
oneTF.delegate=self;// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// Custom input view for the above text fieldUIView*oneInputView =[[UIView alloc] initWithFrame:CGRectMake(0,0,320,100)];
oneInputView.backgroundColor =[UIColor redColor];UIButton*oneReturnB =[UIButton buttonWithType:UIButtonTypeRoundedRect];
oneReturnB.frame =CGRectMake(10,10,300,30);[oneReturnB setTitle:@"return" forState:UIControlStateNormal];[oneReturnB addTarget:self
action:@selector(oneReturnTouchDown:)
forControlEvents:UIControlEventTouchDown];[oneInputView addSubview:oneReturnB];
oneTF.inputView = oneInputView;[oneInputView release];[self.view addSubview:oneTF];// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =// Two text field with 200 height keyboard
twoTF =[[UITextField alloc] initWithFrame:CGRectMake(10,50,300,30)];
twoTF.borderStyle =UITextBorderStyleRoundedRect;
twoTF.text =@"200";
twoTF.delegate=self;// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// Custom input view for the above text fieldUIView*twoInputView =[[UIView alloc] initWithFrame:CGRectMake(0,0,320,200)];
twoInputView.backgroundColor =[UIColor blueColor];UIButton*twoReturnB =[UIButton buttonWithType:UIButtonTypeRoundedRect];
twoReturnB.frame =CGRectMake(10,10,300,30);[twoReturnB setTitle:@"return" forState:UIControlStateNormal];[twoReturnB addTarget:self
action:@selector(twoReturnTouchDown:)
forControlEvents:UIControlEventTouchDown];[twoInputView addSubview:twoReturnB];
twoTF.inputView = twoInputView;[twoInputView release];[self.view addSubview:twoTF];// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =// normal text field with normal keyboard (216 height keyboard)
normalTF =[[UITextField alloc] initWithFrame:CGRectMake(10,90,300,30)];
normalTF.borderStyle =UITextBorderStyleRoundedRect;
normalTF.text =@"normal";
normalTF.delegate=self;[self.view addSubview:normalTF];// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =// Label that displays the keyboard height from keyboardWillShowNotification
keyboardWillShowNotificationL =[[UILabel alloc] initWithFrame:CGRectMake(10,130,300,30)];
keyboardWillShowNotificationL.font =[UIFont systemFontOfSize:14];
keyboardWillShowNotificationL.text =@"keyboardWillShowNotification kbHeight:";[self.view addSubview:keyboardWillShowNotificationL];// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =// Label that displays the keyboard height from keyboardDidShowNotification
keyboardDidShowNotificationL =[[UILabel alloc] initWithFrame:CGRectMake(10,170,300,30)];
keyboardDidShowNotificationL.font =[UIFont systemFontOfSize:14];
keyboardDidShowNotificationL.text =@"keyboardDidShowNotification kbHeight:";[self.view addSubview:keyboardDidShowNotificationL];// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =// Register for keyboard notifications.[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillShowNotification:)
name:UIKeyboardWillShowNotificationobject:nil];[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardDidShowNotification:)
name:UIKeyboardDidShowNotificationobject:nil];returnself;}-(void) dealloc
{// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =// Deregister for keyboard notifications[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardWillShowNotificationobject:nil];[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardDidShowNotificationobject:nil];[oneTF release];[twoTF release];[normalTF release];[keyboardWillShowNotificationL release];[keyboardDidShowNotificationL release];[super dealloc];}-(BOOL) textFieldShouldReturn:(UITextField*)textField
{[textField resignFirstResponder];return YES;}-(void) oneReturnTouchDown:(id)sender
{[oneTF.delegate textFieldShouldReturn:oneTF];}-(void) twoReturnTouchDown:(id)sneder
{[twoTF.delegate textFieldShouldReturn:twoTF];}-(void) keyboardWillShowNotification:(NSNotification*)aNotification
{NSDictionary*info =[aNotification userInfo];CGFloat kbHeight =[[info objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size.height;NSString*string=[[NSString alloc] initWithFormat:@"keyboardWillShowNotification kbHeight: %.2f", kbHeight];NSLog(@"%@",string);
keyboardWillShowNotificationL.text =string;[string release];}-(void) keyboardDidShowNotification:(NSNotification*)aNotification
{NSDictionary*info =[aNotification userInfo];CGFloat kbHeight =[[info objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size.height;NSString*string=[[NSString alloc] initWithFormat:@"keyboardDidShowNotification kbHeight: %.2f", kbHeight];NSLog(@"%@",string);
keyboardDidShowNotificationL.text =string;[string release];}@end
asked Jul 25 '11 at 20:54
mmorris
2,92121225
 

add comment

1 Answer

up vote
11
down vote

accepted

As reported in this question, the start frame (keyed by UIKeyboardFrameBeginUserInfoKey) is where the keyboard is at the beginning of the animation. UIKeyboardFrameEndUserInfoKey should get you the end frame instead. Presumably the size is also different between the frames.

Key reference: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/doc/constant_group/Keyboard_Notification_User_Info_Keys

实时获取键盘高度 CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;的更多相关文章

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

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

  2. js如何获取键盘高度

    在移动端或混合app开发中,js如何获取键盘高度,直接贴上代码吧 input是一个html input 标签 var timer = { id:null, run:function (callback ...

  3. react native 之 获取键盘高度

    多说不如多撸: /** * Created by shaotingzhou on 2017/2/23. *//** * Sample React Native App * https://github ...

  4. iOS、Xcode监测键盘的显示和隐藏变化,并获得键盘高度,改变tableView的frame和偏移

    <pre name="code" class="objc"><pre name="code" class="ob ...

  5. iOS键盘中英文切换键盘高度获取通知方法

    iOS键盘中英文切换键盘高度获取通知方法, 有需要的朋友可以参考下. 注册通知 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppea ...

  6. iOS之 利用通知(NSNotificationCenter)获取键盘的高度,以及显示和隐藏键盘时修改界面的注意事项

    我们在开发中会遇到这样的情况:调用键盘时需要界面有一个调整,避免键盘遮掩输入框. 但实现时你会发现,在不同的手机上键盘的高度是不同的.这里列举一下: //获取键盘的高度 /* iphone 6: 中文 ...

  7. iOS键盘高度的获取

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

  8. Swift3.0 键盘高度监听获取

    方法:通过通知监听键盘的动态 1.键盘的动态有四种: public static let UIKeyboardWillShow: NSNotification.Name public static l ...

  9. iOS键盘高度自适应(中英文输入)

    一:添加通知监测键盘高度变化 [self keyBoardAutoSize]; 二:动态改变高度 #pragma mark keyboard height auto /* NSNotification ...

随机推荐

  1. C++系统学习之七:类

    类的基本思想是数据抽象和封装. 数据抽象是一种依赖于接口和实现分离的编程技术.类的接口包括用户所能执行的操作:类的实现包括类的数据成员.负责接口实现的函数体以及定义类所需的各种私有函数. 封装实现了类 ...

  2. LeetCode 最大正方形

    在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4解法:判 ...

  3. PHP 线上项目 无法操作

    部署到线上的项目,http 环境没有问题,首页展示没有问题,但是跳转页面展示到了本地, 解决办法 : 更改文件夹所属用户 chown -R apache:apache html

  4. 【Java_基础】cmd下使用java命令运行class文件提示“错误:找不到或无法加载主类“的问题分析

    1.问题如下 当在命令行使用java命令执行字节码文件时提示“错误:找不到或无法加载主类” 2. 问题分析 这是由于在运行时类的全名应该是包名+类名,例如在包net.xsoftlab.baike下的类 ...

  5. (转)Objective-C语言--属性和实例变量

    本文转自http://blog.csdn.net/addychen/article/details/39525681 使用Objective-C一段时间了,一直没有弄清楚在Objective-C中属性 ...

  6. 如何在eclipse中引用第三方jar包

    在用UiAutomator做手机自动化测试过程中,在UiAutomator的基础之上进一步封装了里边的方法,以使case开发更顺手.直接在工程的根目录下新建了个libs的文件夹,把封装好的框架打成ja ...

  7. PYDay1-洗剑

    学习语言的阶段: 第一阶段:所有东西都是新的::一个月 第二阶段:开始懂一些::一个月 第三阶段:感觉自己是不可战胜的:第三~第四个月 第四阶段:突然感觉什么都不知道,开发是无止境的::培训阶段不会遇 ...

  8. iOS学习笔记22-推送通知

    一.推送通知 推送通知就是向用户推送一条信息来通知用户某件事件,可以在应用退到后台后,或者关闭后,能够通过推送一条消息通知用户某件事情,比如版本更新等等. 推送通知的常用应用场景: 一些任务管理APP ...

  9. Get 了滤镜、动画、AR 特效,速来炫出你的短视频开发特技!

    在滤镜美颜.搞怪特效.炫酷场景等各种新奇玩法驱动下,短视频开始让人上瘾. 12 月 3 日,七牛云联合八大短视频特效平台共同推出了中国短视频开发者创意大赛(China Short Video Cont ...

  10. 【Luogu】P2680运输计划(树上差分+二分)

    题目链接 总体思路……怎么说呢……是个暴力吧…… 首先用倍增预处理出每条路径的长度. 然后按长度把路径排序. 然后二分答案.对于当前答案mid检验,怎么检验呢? 首先差分把所有长度比mid大的链上除了 ...