gpt4 book ai didi

ios - 当键盘中的 "Next"返回按钮时滚动带有文本字段的 TableView

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:11:35 25 4
gpt4 key购买 nike

我有一个包含自定义单元格的分组 TableView ,这些单元格有一个 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 ;

但我无法滚动 TableView 内容以使当前事件文本字段在键盘上方可见。当我没有在键盘中实现“下一步”按钮功能时,我成功地管理了这个,当点击“输入”按钮时,键盘只是隐藏了,然后当另一个文本字段被点击时,键盘再次显示,因为我正在听UIKeyboardDidShowNotificationUIKeyboardDidHideNotification。但是现在我想要键盘上的“下一步”按钮并且在点击按钮时键盘没有被隐藏,我不知道如何正确滚动到事件文本字段。

提前致谢

最佳答案

这是实现此目的最简单有效的方法:

添加以下常量:

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;

将此添加到您的 View Controller :

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 .

关于ios - 当键盘中的 "Next"返回按钮时滚动带有文本字段的 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18297929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com