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 ?
            Asked
            
        
        
            Active
            
        
            Viewed 3.9k times
        
    25
            
            
        - 
                    `textFieldDidBecomeActive` > move text field accordingly. – Aug 14 '13 at 05:55
- 
                    http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present please see this link – BhavikKama Aug 14 '13 at 05:58
- 
                    2Is the search that hard to use? Even if it is, look at the sidebar: it shows you related questions. – Abizern Aug 14 '13 at 07:14
5 Answers
21
            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...
- 
                    2Hum, you should use block for animation, your method is discouraged since iOS4 – Beuj Oct 04 '13 at 13:59
- 
                    1I faced lot of issues with this code. solved it by replacing 'self.inputView.frame' with 'self.view.frame' . – Rajesh Jul 07 '14 at 12:40
- 
                    When you cannot use solution with scrollview, this is the easiest and best way – Alex Sorokoletov Oct 28 '14 at 20:36
- 
                    
5
            
            
        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.
 
    
    
        Iducool
        
- 3,543
- 2
- 24
- 45
5
            
            
        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.
3
            
            
        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];
}
 
    
    
        iPhone 7
        
- 1,731
- 1
- 27
- 63
2
            
            
        #define kOFFSET_FOR_KEYBOARD 80.0
-(void)keyboardWillShow {
    // Animate the current view out of the way
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}
-(void)keyboardWillHide {
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if ([sender isEqual:mailTf])
    {
        //move the main view, so that the keyboard does not hide it.
        if  (self.view.frame.origin.y >= 0)
        {
            [self setViewMovedUp:YES];
        }
    }
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view
    CGRect rect = self.view.frame;
    if (movedUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
        // 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.view.frame = rect;
    [UIView commitAnimations];
}
- (void)viewWillAppear:(BOOL)animated
{
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}
try This CODE.....
 
    
    
        Maulik Kundaliya
        
- 452
- 3
- 17
- 
                    
- 
                    2Annnnnddd you should use block for animation, your method is discouraged since iOS4... – Beuj Oct 04 '13 at 13:59
- 
                    
- 
                    ...and you shouldn't use a constant for the keyboard size, you're encouraged to get the offset size from Apple. – Matt Parkins Sep 22 '14 at 15:33
 
     
     
    