Mads Odgaard
Mads Odgaard

Reputation: 47

UITableView move up when keyboard is active

I have UITableView with UITextfields as the subview in contentView.

They work and all, but when I select the bottom ones, they are getting covered by the keyboard, so I want the UITableView to scroll up.

Here is my current code:

-(void)keyboardWillShow:(NSNotification*)notification {
NSValue *keyboardFrameValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrame = [[self view] convertRect:[keyboardFrameValue CGRectValue] fromView:nil];

[UIView animateWithDuration:0.3 animations:^{
    [constraint setConstant:keyboardFrame.size.height];
    [self.myTableView layoutIfNeeded];
}];
}

-(void)keyboardWillHide:(NSNotification *)notification {
[UIView animateWithDuration:0.3 animations:^{
    [constraint setConstant:0];
    [self.myTableView layoutIfNeeded];
}];
}

The problem with this is that it messes up the data, textfields are being added to the wrong Cells and stuff.

Hope that anyone can help out.

Upvotes: 1

Views: 82

Answers (1)

florianbuerger
florianbuerger

Reputation: 422

If possible, use an UITableViewController, the keyboard is handled automatically.

Otherwise, adjust the contentInset property of the table view as outlined by Apple.

Upvotes: 1

Related Questions