键盘出现时查看不向上移动

时间:2013-09-03 10:20:31

标签: ios

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

2 个答案:

答案 0 :(得分:14)

试试这个:

[[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)

答案 1 :(得分:1)

你有没有试过 textFieldDidBeginEditing 我不知道你是否尝试过这种方式以及是否正确。我用它来减少线路来实现

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

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

}

这会将您的根视图移到顶部,这样您的子视图会自动移动到顶部文本字段,不会隐藏在键盘后面,抱歉我没有评论的声誉