UIKeyboardWillShowNotification错误地更改了UIWebView的框架

时间:2014-01-07 05:20:29

标签: ios objective-c uiwebview nsnotificationcenter uikeyboard

我正在使用UIKeyboardWillShowNotification来了解键盘何时显示并调整UIWebView的大小,以便它不会隐藏在键盘后面。

奇怪的是,当我更改NSNotificationCenter调用的方法中的帧时,它会以允许我滚动UIWebView内容(屏幕截图中为红色)的方式更改帧,但是还有很大一部分UIWebView滚动到视图中(屏幕截图中为黄色)。黄色永远不应该出现。

enter image description here

- (void)keyboardWillShowOrHide:(NSNotification *)notification {

    // User Info
    NSDictionary *info = notification.userInfo;
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    int curve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
    CGRect keyboard = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];

    if ([notification.name isEqualToString:UIKeyboardWillShowNotification]) {
        [UIView animateWithDuration:duration delay:0 options:curve animations:^{
            CGRect frame = self.toolbarHolder.frame;
            frame.origin.y = (self.view.frame.size.height - keyboard.size.height) - 44;
            self.toolbarHolder.frame = frame;

            // Editor View
            CGRect editorFrame = self.editorView.frame;
            editorFrame.size.height = (self.view.frame.size.height - keyboard.size.height) - 44;
            self.editorView.frame = editorFrame;

        } completion:nil];
    } else {
        [UIView animateWithDuration:duration delay:0 options:curve animations:^{
            CGRect frame = self.toolbarHolder.frame;
            frame.origin.y = self.view.frame.size.height;
            self.toolbarHolder.frame = frame;

            // Editor View
            CGRect editorFrame = self.editorView.frame;
            editorFrame.size.height = self.view.frame.size.height;
            self.editorView.frame = editorFrame;

        } completion:nil];
    }

}

如果我使用与UIWebView调用的方法不同的方法更改NSNotificationCenter框架,则框架会正确更改,键盘上方的区域仅填充{{1 (红色)。

enter image description here

可能导致此问题的原因是什么?

1 个答案:

答案 0 :(得分:2)

使用UIKeyboardFrameEndUserInfoKey键返回键盘的最终预期帧