繁体   English   中英

NSNotificationCenter

[英]NSNotificationCenter

现在我正在尝试编写一个 function 来在屏幕上出现键盘时向上移动框架。 我开始使用 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];

}

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

暂无
暂无

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

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