Get UITableView to scroll to the selected UITextField and Avoid Being Hidden by Keyboard

后端 未结 13 1004
鱼传尺愫
鱼传尺愫 2020-11-29 17:47

I have a UITextField in a table view on a UIViewController (not a UITableViewController). If the table view is on a UITableViewC

13条回答
  •  半阙折子戏
    2020-11-29 18:22

    In my case my UITableview was inside another UIView and that UIvie was in the main UIScrollview. So I used more general solution for those kind of problems. I simply found the Y coordinate of my cell in the specific UIScrollView and then scrolled to correct point:

    -(void)textFieldDidBeginEditing:(UITextField *)textField{
    float kbHeight = 216;//Hard Coded and will not support lanscape mode
    UITableViewCell *cell = (UITableViewCell *)[textField superview];
    float scrollToHeight = [self FindCordinate:cell];
    [(UIScrollView *)self.view setContentOffset:CGPointMake(0, scrollToHeight - kbHeight + cell.frame.size.height) animated:YES];
    }
    
    -(float)FindCordinate:(UIView *)cell{
    float Ycordinate = 0.0;
    while ([cell superview] != self.view) {
        Ycordinate += cell.frame.origin.y;
        cell = [cell superview];
    }
    Ycordinate += cell.frame.origin.y;
    return Ycordinate;
    }
    

提交回复
热议问题