1

现在我正在尝试编写一个函数来在屏幕上出现键盘时向上移动框架。我开始使用 NSNNotificationCenter。我的代码工作但不正确。当键盘出现时,我的 formView 正在向上移动,但是当我开始在 formView 中编辑下一个 textField 时,formView 再次向上移动。我的代码有什么问题?谢谢。

- (void)keyboardWillShow:(NSNotification *) aNotification {
    NSDictionary *userInfo = [aNotification userInfo];

    CGRect frame = self.formView.frame;
    frame.origin.y -= 170;

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:animationDuration];

    formView.frame = frame;

    [UIView commitAnimations];

}
4

1 回答 1

1

origin.y当键盘消失时,您应该再次将 170 像素(或 Mike 建议的任何计算)添加到视图中。当您单击另一个文本字段时,从技术上讲,当前键盘将消失(您的视图不会以任何方式做出反应)并且会出现一个新键盘(您keyboardWillShow将再次被调用并且您再次将视图向上移动 170 像素)。

于 2012-04-04T09:53:59.053 回答