In my app, I have a UITableView which has some custom cells and in those I have placed UITextField. That UITextField is sometimes getting blocked my the keyboard which is a issue. So in my situation I am trying to move the whole tableview itself instead of just scrolling to a particular cell which was an issue for me.
Anyway I am having issues this way too.
Anyway this is my code for the textField showing method and ending method. The problem is that the UITableView never moves the right amount. I am just trying to move the UITableview up enough so that the cell that the UITextField that is currently being invoked, is just above the keyboard. So sometimes it scrolls up too much where I can’t see the cell or the whole tableview moves a crazy amount where I do not see it at all.
Anyway here is my code, does anyone see anything wrong?
- (BOOL)textFieldShouldBeginEditing:(UITextField *)thetextField {
CustomCell *cell = (CustomCell*)[[thetextField superview] superview];
CGRect cellRect = [cell convertRect:cell.frame toView:self.view];
float bottomCell = cellRect.origin.y - cellRect.size.height;
if (bottomCell >= keyboardY) {
subtractionAmount = keyboardY - bottomCell;
didMove = YES;
[UIView animateWithDuration:0.4
animations:^{
thetableView.center = CGPointMake(thetableView.center.x, thetableView.center.y + subtractionAmount);
}];
}
return YES;
}
-(BOOL) textFieldShouldReturn:(UITextField *)thetextField {
if (didMove) {
didMove = NO;
[UIView animateWithDuration:0.4
animations:^{
thetableView.center = CGPointMake(thetableView.center.x, thetableView.center.y - subtractionAmount);
}];
}
Thanks!
If I undesrtood correctly your problem, I encountered that problem one month ago. My scenario was the following: a
UITableViewwithUITextFields without aUITableViewController.I fixed the problem with the following tutorial: sliding-uitextfields-around-to-avoid.
Hope it helps.