简体   繁体   English

iOS 8键盘 - 显示键盘时向上推UITextField的问题

[英]iOS 8 Keyboard - Issue with pushing the UITextField up when keyboard was shown

I have a message like chat window. 我有一个像聊天窗口的消息。 When the keyboard was shown, I would push the UITextField so it appears to be right above the keyboard. 当键盘显示时,我会按下UITextField,使其显示在键盘正上方。 In iOS 7, it works fine, but it doesn't work on iOS 8. Here's my code 在iOS 7中,它工作正常,但它在iOS 8上不起作用。这是我的代码

// Detect the keyboard events

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];

// Listen to keyboard shown/hidden event and resize. 

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView animateWithDuration:0.2f animations:^{

        CGRect frame = self.textInputView.frame;
        frame.origin.y -= kbSize.height;
        self.textInputView.frame = frame;

        frame = self.myTableView.frame;
        frame.size.height -= kbSize.height;
        self.myTableView.frame = frame;
    }];
    [self scrollToTheBottom:NO];
}


- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
     NSDictionary* info = [aNotification userInfo];
     CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView animateWithDuration:0.2f animations:^{

        CGRect frame = self.textInputView.frame;
        frame.origin.y += kbSize.height;
        self.textInputView.frame = frame;

        frame = self.myTableView.frame;
        frame.size.height += kbSize.height;
        self.myTableView.frame = frame;
    }];
}

You're using UIKeyboardFrameBeginUserInfoKey , in order for your code to work, you should be using UIKeyboardFrameEndUserInfoKey 您正在使用UIKeyboardFrameBeginUserInfoKey ,为了使您的代码正常工作,您应该使用UIKeyboardFrameEndUserInfoKey

UIKeyboardFrameBeginUserInfoKey The key for an NSValue object containing a CGRect that identifies the start frame of the keyboard in screen coordinates. UIKeyboardFrameBeginUserInfoKey包含CGRect的NSValue对象的键,用于标识屏幕坐标中键盘的起始帧。 These coordinates do not take into account any rotation factors applied to the window's contents as a result of interface orientation changes. 这些坐标不考虑由于界面方向改变而应用于窗口内容的任何旋转因子。 Thus, you may need to convert the rectangle to window coordinates (using the convertRect:fromWindow: method) or to view coordinates (using the convertRect:fromView: method) before using it. 因此,您可能需要在使用之前将矩形转换为窗口坐标(使用convertRect:fromWindow:方法)或查看坐标(使用convertRect:fromView:方法)。

UIKeyboardFrameEndUserInfoKey The key for an NSValue object containing a CGRect that identifies the end frame of the keyboard in screen coordinates. UIKeyboardFrameEndUserInfoKey包含CGRect的NSValue对象的键,用于标识屏幕坐标中键盘的结束帧。 These coordinates do not take into account any rotation factors applied to the window's contents as a result of interface orientation changes. 这些坐标不考虑由于界面方向改变而应用于窗口内容的任何旋转因子。 Thus, you may need to convert the rectangle to window coordinates (using the convertRect:fromWindow: method) or to view coordinates (using the convertRect:fromView: method) before using it. 因此,您可能需要在使用之前将矩形转换为窗口坐标(使用convertRect:fromWindow:方法)或查看坐标(使用convertRect:fromView:方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM