Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIKeyboardWillShowNotification&;UIKeyboardDidShowNotification报告键盘高度错误_Ios_Keyboard_Size_Uikeyboard_Nsnotificationcenter - Fatal编程技术网

Ios UIKeyboardWillShowNotification&;UIKeyboardDidShowNotification报告键盘高度错误

Ios UIKeyboardWillShowNotification&;UIKeyboardDidShowNotification报告键盘高度错误,ios,keyboard,size,uikeyboard,nsnotificationcenter,Ios,Keyboard,Size,Uikeyboard,Nsnotificationcenter,下面的代码(长度很抱歉)在iOS 4.3下显示了一个奇怪的行为(可能还有其他版本)。在本例中,有三个UITextFields具有三个不同大小的键盘。如果您开始编辑一个文本字段,然后触摸“返回”关闭键盘,则每次使用uiKeyboardFrameBeginUserInfo键在UIKeyboardWillShowNotification和UIKeyboardDidShowNotification中正确返回键盘大小时 见下文: -(无效)键盘将显示通知:(NSNotification*)通知 及 -(无

下面的代码(长度很抱歉)在iOS 4.3下显示了一个奇怪的行为(可能还有其他版本)。在本例中,有三个
UITextField
s具有三个不同大小的键盘。如果您开始编辑一个文本字段,然后触摸“返回”关闭键盘,则每次使用
uiKeyboardFrameBeginUserInfo键在
UIKeyboardWillShowNotification
UIKeyboardDidShowNotification
中正确返回键盘大小时

见下文:

-(无效)键盘将显示通知:(NSNotification*)通知

-(无效)键盘DIDSHOWNOTITION:(NSNotification*)加密

请注意,这是预期的行为

action                 reported keyboard size  expected keyboard size  
---------------------  ----------------------  ----------------------
touch one    & return  100                     100
touch two    & return  200                     200
touch normal & return  216                     216
n            & return  keyboard size(n)        keyboard size(n)
意外行为是,如果开始编辑文本字段,将报告(预期)第一个键盘的大小。触摸第二个文本字段(不触摸回车)时,会报告第一个键盘的大小(意外),而不是第二个键盘的大小。触摸第三个文本字段(不触摸回车)时,会报告(意外)第二个键盘大小,而不是第三个键盘大小。第二次到第n次,它似乎在报告上一个键盘的大小,而不是将要显示的大小

action        reported keyboard size  expected keyboard size  
------------  ----------------------  ----------------------
touch one     100                     100
touch two     100                     200
touch normal  200                     216
touch one     216                     100
n             keyboard size(n-1)      keyboard size(n)
在我发送错误报告之前,我只想确保我没有过度查看任何内容

仅供参考,在移动视图时,我在尝试做正确的事情(使用
UIKeyboardWillShowNotification
UIKeyboardDidShowNotification
UIKeyboardFrameBeginUserInfo
以获取键盘大小)时,我在这一点上打了个短文,以便可以看到可能被键盘遮挡的文本字段。参考:

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
如中所述,
start frame
(由
UIKeyboardFrameBeginUserInfo键设置)是键盘位于动画开头的位置
UIKeyboardFrameEndUserInfoKey
应该为您提供
结束帧
。大概帧之间的大小也不同


密钥参考:

感谢您指出我应该使用UIKeyboardFrameEndUserInfoKey。当我最初查看文档时,并不清楚UIKeyboardFrameBeginUserInfoKey和UIKeyboardFrameEndUserInfoKey之间的区别。谢谢你的解释,太好了!这是一个晦涩难懂的案例,我刚刚遇到了标准键盘和日期选择器的混合,谢天谢地找到了你的帖子。UIKeyboardFrameEndUserInfoKey是所需的关键信息!!!
#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