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

后端 未结 13 981
鱼传尺愫
鱼传尺愫 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:09

    In my app, I have successfully used a combination of contentInset and scrollToRowAtIndexPath like this:

    When you want to display the keyboard, just add a contentInset at the bottom with your table with desired height:

    tableView.contentInset =  UIEdgeInsetsMake(0, 0, height, 0);
    

    Then, you can safely use

    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:cell_index inSection:cell_section] animated:YES];
    

    By adding the contentInset, even if you are focusing on the last cell the tableView will still be able to scroll. Just make sure that when you are dismissing the keyboard, you reset the contentInset.

    EDIT:
    If you have only one section (you can replace cell_section with 0) and the use the textView tag to inform the cell row.

提交回复
热议问题