如何让自定义 UITableView 自动滚动到选定的文本字段?

发布于 2024-09-04 21:14:06 字数 286 浏览 1 评论 0原文

我有一个 UITableView,我通过自定义 UIViewController 控制它。当用户单击“添加”按钮时,我向 UITableView 添加一行,其中包含一个文本字段,并使其成为第一响应者。问题是,当表格底部不可见(或被键盘隐藏)时,UITableView 不会滚动以使文本字段进入视图。

UITableViewController 自动执行此操作,但我的视图控制器不能是 UITableViewController 的子类。

I have a UITableView, which I am controlling from a custom UIViewController. When the user clicks the 'add' button, I add a row to the UITableView with a text field in it, and make it the first responder. The problem is, when the bottom of the table is out of view (or hidden by the keyboard), the UITableView doesn't scroll to bring the text field into view.

UITableViewController does this automatically, but my View Controller can't be a subclass of UITableViewController.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴情换悲伤 2024-09-11 21:14:06

当键盘出现或消失时,我通过在 UITableView 上旋转 contentInset 来修复此问题。

- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasHidden:)
                                                 name:UIKeyboardDidHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)aNotification {
    CGRect keyboardBounds;
    [[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
    keyboardHeight = keyboardBounds.size.height;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);
    [UIView commitAnimations];
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[items count] inSection:0]
                     atScrollPosition:UITableViewScrollPositionMiddle
                             animated:YES];
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    tableView.contentInset = UIEdgeInsetsZero;
    [UIView commitAnimations];
}

当您加载 UITableView 时调用 registerForKeyboardNotifications,其他一切都应该正常工作。

I fixed this by twiddling contentInset on the UITableView when the keyboard appears or disappears.

- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasHidden:)
                                                 name:UIKeyboardDidHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)aNotification {
    CGRect keyboardBounds;
    [[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
    keyboardHeight = keyboardBounds.size.height;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);
    [UIView commitAnimations];
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[items count] inSection:0]
                     atScrollPosition:UITableViewScrollPositionMiddle
                             animated:YES];
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    tableView.contentInset = UIEdgeInsetsZero;
    [UIView commitAnimations];
}

call registerForKeyboardNotifications when you load the UITableView, and everything else should Just Work.

夏尔 2024-09-11 21:14:06

我发现 -scrollToRowAtIndexPath:atScrollPosition:animated: 并猜测这会做你想要的。

I found -scrollToRowAtIndexPath:atScrollPosition:animated: and guess this would do what you want.

嘦怹 2024-09-11 21:14:06

适用于ios5

UITextField *activeField;

// Called when the UIKeyboardDidShowNotification is sent.

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
   activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
   activeField = nil;
}

// Called when the UIKeyboardWillHideNotification is sent

- (void)keyboardWasShown:(NSNotification *)aNotification {


   id view = [activeField superview];
   while (view && ![view isKindOfClass:[UITableViewCell class]]) 
   {
      view = [view superview];
   }
   UITableViewCell *cell = view;
   NSIndexPath *indexPath = [tableview indexPathForCell:cell];
   CGRect keyboardBounds;
   [[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationBeginsFromCurrentState:YES];
   tableview.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);
   [UIView commitAnimations];
   [tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationBeginsFromCurrentState:YES];
    tableview.contentInset = UIEdgeInsetsZero;
   [UIView commitAnimations];
}

work for ios5

UITextField *activeField;

// Called when the UIKeyboardDidShowNotification is sent.

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
   activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
   activeField = nil;
}

// Called when the UIKeyboardWillHideNotification is sent

- (void)keyboardWasShown:(NSNotification *)aNotification {


   id view = [activeField superview];
   while (view && ![view isKindOfClass:[UITableViewCell class]]) 
   {
      view = [view superview];
   }
   UITableViewCell *cell = view;
   NSIndexPath *indexPath = [tableview indexPathForCell:cell];
   CGRect keyboardBounds;
   [[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationBeginsFromCurrentState:YES];
   tableview.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);
   [UIView commitAnimations];
   [tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationBeginsFromCurrentState:YES];
    tableview.contentInset = UIEdgeInsetsZero;
   [UIView commitAnimations];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文