Pushparaj
Pushparaj

Reputation: 415

UITableView slightly goes up when keyboard hides

enter image description here

I am using UITableView (chatTable) along with UITabBar (chatTabBar) and one textField inside imageView. I am using autolayout. I used the following code to change the views when keyboard appears and disappears.

        - (void)keyboardWasShown:(NSNotification*)aNotification
        {
            NSDictionary* info = [aNotification userInfo];

            // get animation info from userInfo
            NSTimeInterval animationDuration;
            CGRect keyboardFrame;
            [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
            [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];

            // resize the frame
            [UIView animateWithDuration:animationDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{

                self.keyboardHeight.constant =  keyboardFrame.size.height - TABBAR_HEIGHT ;
                [self.view layoutIfNeeded];
            } completion:nil];

            if ([chatData count] != VALUE_ZERO)
            {
                [chatTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([chatData count] - VALUE_ONE) inSection:VALUE_ZERO] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
            }
        }

        - (void)keyboardWillHide:(NSNotification*)aNotification
        {
            NSDictionary* info = [aNotification userInfo];

            // get animation info from userInfo
            NSTimeInterval animationDuration;
            CGRect keyboardFrame;
            [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
            [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];

            // Set view frame
            [UIView animateWithDuration:animationDuration delay:2.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                self.keyboardHeight.constant -= keyboardFrame.size.height - TABBAR_HEIGHT;
                [self.view layoutIfNeeded];
            } completion:nil];
        }

Now when I press return the tableview goes up a littel bit (from screen 2 to screen 3). keyboardHeight is the bottom space constraint between the tabBar and main view.

enter image description here (screen 2)


enter image description here (screen3)

I have tried many things but I can't able to find why the tableview is going up for a while. (problem is there is no smooth animation.) (Note: I have put delay as 2.0 only to show what happens in following screenshot(screen 3) othewise it's value would be 0)

Upvotes: 1

Views: 862

Answers (2)

Pushparaj
Pushparaj

Reputation: 415

Solved the problem with contentInset property. I am using contentInset as mentiond by the @Eugene and also changing the constant property of bottom constraint of the textfiled to move up and doen whenever keyboard is shown and hidden.

Upvotes: 0

Eugene
Eugene

Reputation: 10045

Your problem is that you're changing the table view frame when the keyboard appears, which is wrong. You need to change the contentInset property of the table view, instead of meddling with frames.

- (void)keyboardWillShow:(NSNotification *)notification {
  CGFloat height = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height - self.tabBarController.tabBar.frame.size.height;
  UIEdgeInsets edgeInsets = UIEdgeInsetsMake(0.0f, 0.0f, height, 0.0f);
  _tableView.contentInset = edgeInsets;
  _tableView.scrollIndicatorInsets = edgeInsets;
}

- (void)keyboardWillHide:(NSNotification *)notification {
  UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
  _tableView.contentInset = edgeInsets;
  _tableView.scrollIndicatorInsets = edgeInsets;
}

Upvotes: 4

Related Questions