0

I have added one tableview inside xib. After that I have added headerview and footerview in that. Inside footerview there is one textview. Textview can't scroll to visible area once keyboard appears.

Let me know anyone facing the same issue.

Bhadresh Mulsaniya
  • 2,610
  • 1
  • 12
  • 25
  • You mean your textView is hiding behind the keyboard? – Bharat Modi Jun 16 '16 at 13:00
  • There is one good library available on Git which is my personal favourite. TPKeyboardAvoiding - https://github.com/michaeltyson/TPKeyboardAvoiding. It is super easy to use just add the files to the project and set TPKeyboardAvoidingTableView class to your tableView's CustomClass. – Bharat Modi Jun 16 '16 at 13:20

2 Answers2

0

This behavior is not handled by default. You have to implement it yourself.

The other solution is to use UITableViewController instead of UITableView, which implements this behavior by default. You could put the UITableViewController in a UIContainerView if you don't want it to take the whole frame.

See code here if you want to implement the behavior yourself How to make a UITextField move up when keyboard is present?

Hope it helps.

Community
  • 1
  • 1
AnthoPak
  • 4,191
  • 3
  • 23
  • 41
0

Here is some sample code:

  #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
{
    [super viewWillAppear: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
{
    [super viewWillDisappear:animated];
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}
vipinsaini0
  • 541
  • 1
  • 7
  • 26