0

我在子视图中有一个 UI 文本字段。我将它添加到视图中。当键盘弹出时,我想为我添加此子视图的视图设置动画。我已经在我的子视图类中编写了所有文本字段委托函数只有。所以如果我使用动画文本字段功能,它不会移动父视图,而是你的子视图是动画的....请帮助我

4

2 回答 2

15

尝试这个:

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

在里面viewDidLoad

然后这个

- (void)keyboardWillHide:(NSNotification *)aNotification
{
    // the keyboard is hiding reset the table's height
    NSTimeInterval animationDuration =
    [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect frame = self.view.frame;
    frame.origin.y += 160;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    // the keyboard is showing so resize the table's height
    NSTimeInterval animationDuration =
    [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect frame = self.view.frame;
    frame.origin.y -= 160;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}

在您的视图控制器中

您很可能必须根据您的具体观点更改我在此处放置的值 (160)

于 2013-09-03T10:29:55.063 回答
1

您尝试过textFieldDidBeginEditing我不知道您是否尝试过,以及它是否正确。我用它来实现更少的线路

-(void)textFieldDidBeginEditing:(UITextField *)textField
{

    self.view.frame=CGRectMake(0, -300, 320, 700);

}

这会将您的根视图移至顶部,因此您的子视图将自动移至顶部文本字段不会隐藏在键盘后面,对不起,我没有评论的声誉

于 2013-09-03T10:56:28.777 回答