UIKeyboardWillShowNotification & UIKeyboardDidShowNotification report wrong keyboard height

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 UIKeyboardDidShowNotificationusing 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 & return 100 100touch two & return 200 200touch normal & return 216 216n & 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 100 100touch two 100 200touch normal 200 216touch one 216 100n 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 orUIKeyboardDidShowNotification 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>


@interface BugVC : 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"


@implementation BugVC


- (id) init

{

    if ( !(self = [super init]) )

    {

        return self;

    }


    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

    // 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 field

    UIView *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 field

    UIView *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:UIKeyboardWillShowNotification object:nil];


    [[NSNotificationCenter defaultCenter]

     addObserver:self

        selector:@selector(keyboardDidShowNotification:)

            name:UIKeyboardDidShowNotification object:nil];


    return self;

}


- (void) dealloc

{

    // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

    // Deregister for keyboard notifications

    [[NSNotificationCenter defaultCenter]

     removeObserver:self

               name:UIKeyboardWillShowNotification object:nil];


    [[NSNotificationCenter defaultCenter]

     removeObserver:self

               name:UIKeyboardDidShowNotification object: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  

******************************************************************************************************************************************************************
******************************************************************************************************************************************************************
1

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 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值