问题
Currently I have a UITextField in each UITableViewCell but the issue is, if there are 2 cells or more the cell could be cut off.
Is there any easy way to make it so that the cell is visible while my keyboard is open?
Thanks!
回答1:
If i'm understanding your question correctly, you want the view to scroll when the keyboard shows up. If you make your viewController a subclass of UITableViewController, I believe you get scrolling for free. The only other option is unfortunately not that simple. You need to play around with the delegation methods to move your view up and down (and animate it if you want it to slide up with the keyboard). There's lots of answers to this you can find so I won't go into the details, here's a few links to help you out!
In depth explation here
Great explanation here
Some UIScrollView details here
More here
回答2:
That worked pretty well for me, in case somebody still need the answer:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if (self.tableView.contentOffset.y == 0)
{
[UIView animateWithDuration:0.0 delay:0.5 options:UIViewAnimationOptionAllowUserInteraction animations:^{
} completion:^(BOOL finished) {
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}];
}
}
The problem was that the keyboard was not taken into consideration when sliding to the cell, so I just added the call in a small delay block.
来源:https://stackoverflow.com/questions/8685800/keyboard-blocking-uitableviewcells-uitextfield