-3

How can i move textView/textField to top of view when I select one of them?

rmaddy
  • 298,130
  • 40
  • 468
  • 517

1 Answers1

2

Working code....

1) set delegate to you textFields

[self.textField setDelegate:self];

2) To move textField up

-(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) To move textField down

-(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) to move textView Up

-(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) to move textView Down

-(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];
    }
}
Ty Lertwichaiworawit
  • 2,930
  • 2
  • 20
  • 41