关于ios:在键盘外观的同时将textView / textField移动到顶部

Moving textView/textField to top while keyboard appearance

本问题已经有最佳答案,请猛点这里访问。

当我选择其中一个时,如何将textViewtextField移到视图顶部?


工作代码….

1)设置delegate为你textFields

1
[self.textField setDelegate:self];

2)向上移动EDOCX1[2]

1
2
3
4
5
6
7
8
9
10
11
12
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.textField)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.view.frame = CGRectMake(self.view.frame.origin.x , (self.view.frame.origin.y - 80), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

3)向下移动EDOCX1[2]

1
2
3
4
5
6
7
8
9
10
11
12
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == self.textField)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.view.frame = CGRectMake(self.view.frame.origin.x , (self.view.frame.origin.y + 80), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

4)向上移动EDOCX1[4]

1
2
3
4
5
6
7
8
9
10
11
12
-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if (textView == self.textView)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.view.frame = CGRectMake(self.view.frame.origin.x , (self.view.frame.origin.y - 80), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

5)向下移动EDOCX1[4]

1
2
3
4
5
6
7
8
9
10
11
12
-(void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView == self.textView)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.view.frame = CGRectMake(self.view.frame.origin.x , (self.view.frame.origin.y + 80), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}