我有一个带有自定义单元格的分组表视图,这些单元格有一个UITextField
。我已将文本字段的returnKeyType
设置为UIReturnKeyNext
(最后一行除外),并且我已通过这种方式实现了textFieldShouldReturn:
方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
id firstResponderCell = textField.superview.superview ;
NSInteger firstResponderRow = [self.tableView indexPathForCell:firstResponderCell].row ;
NSIndexPath* nextCellIndexPath = [NSIndexPath indexPathForRow:firstResponderRow+1 inSection:0] ;
CustomCell* nextCell = (CustomCell*)[self.tableView cellForRowAtIndexPath:nextCellIndexPath] ;
if (nextCell) {
[nextCell.cellTextField becomeFirstResponder] ;
}
else
[textField resignFirstResponder] ;
return YES ;
}
但是我无法滚动表格视图内容,以使当前活动文本字段在键盘上方可见。当我没有实现键盘中的“下一步”按钮功能时我成功地管理了这个,当点击“Enter”按钮时,键盘被隐藏,然后当点击另一个文本字段时再次显示键盘,因为我在听UIKeyboardDidShowNotification
和UIKeyboardDidHideNotification
。但是现在我想要点击按钮时键盘和键盘上的“下一步”按钮不被隐藏,我不知道如何正确滚动到活动文本字段。
提前致谢
答案 0 :(得分:2)
这是实现这一目标的最简单有效的方法:
添加以下常量:
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
将此添加到视图控制器:
CGFloat animatedDistance;
并将这些方法添加到您的代码中:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
P.S:这是来自here。
答案 1 :(得分:0)
这不是获取细胞的安全方法:
id firstResponderCell = textField.superview.superview ;
即。它现在可能有用,但它可能在(近)未来不起作用。
知道索引路径后,应检查它是否确实存在。然后,您可以使用scrollToRowAtIndexPath:atScrollPosition:animated:
滚动行(并使其成为顶行)。或者,您可以获取行的框架,然后设置表视图的contentOffset
。