Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a UITextView move up when keyboard is present [duplicate]

How to move UITextView Up and Down when you start entering some value. Like in TextField we use its delegate method. What to do in case of UITextView ?

like image 349
Bug Avatar asked Aug 14 '13 05:08

Bug


4 Answers

Here is a sample code that will handle the keyboard automatically for you. Keyboard avoiding

If you are using TableView then your tableView must be a subclass of TPKeyboardAvoidingTableView and if you are using scrollview then your scrollview must be a subclass of TPKeyboardAvoidingScrollView. And this library will automatically do keyboard handling for you.

like image 25
Iducool Avatar answered Nov 16 '22 19:11

Iducool


While editing complete View will move up and after done editing will move down...

- (void)textViewDidBeginEditing:(UITextView *)textView
{
   [self animateTextView: YES];
 }

- (void)textViewDidEndEditing:(UITextView *)textView
 {
   [self animateTextView:NO];
  }

- (void) animateTextView:(BOOL) up
    {
        const int movementDistance =heightKeyboard; // tweak as needed
        const float movementDuration = 0.3f; // tweak as needed
        int movement= movement = (up ? -movementDistance : movementDistance);
        NSLog(@"%d",movement);

        [UIView beginAnimations: @"anim" context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
        [UIView commitAnimations];
    }

I hope this will help you...

like image 199
Bhrigesh Avatar answered Nov 16 '22 20:11

Bhrigesh


I suggest you to take a look at this guide

in particular at section: Moving Content That Is Located Under the Keyboard. I have used this approach successfully several times.

like image 33
laucel Avatar answered Nov 16 '22 19:11

laucel


I did by changing the frame.

-(void)textViewDidBeginEditing:(UITextView *)textView{

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewMsg cache:YES];
viewMsg.frame = CGRectMake(10, 50, 300, 200);
[UIView commitAnimations];

NSLog(@"Started editing target!");

}

-(void)textViewDidEndEditing:(UITextView *)textView
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewMsg cache:YES];
viewMsg.frame = CGRectMake(10, 150, 300, 200);
[UIView commitAnimations];
}
like image 41
iPhone 7 Avatar answered Nov 16 '22 18:11

iPhone 7