<bdo id='f2PdE'></bdo><ul id='f2PdE'></ul>

  • <i id='f2PdE'><tr id='f2PdE'><dt id='f2PdE'><q id='f2PdE'><span id='f2PdE'><b id='f2PdE'><form id='f2PdE'><ins id='f2PdE'></ins><ul id='f2PdE'></ul><sub id='f2PdE'></sub></form><legend id='f2PdE'></legend><bdo id='f2PdE'><pre id='f2PdE'><center id='f2PdE'></center></pre></bdo></b><th id='f2PdE'></th></span></q></dt></tr></i><div id='f2PdE'><tfoot id='f2PdE'></tfoot><dl id='f2PdE'><fieldset id='f2PdE'></fieldset></dl></div>

      <tfoot id='f2PdE'></tfoot>
    1. <legend id='f2PdE'><style id='f2PdE'><dir id='f2PdE'><q id='f2PdE'></q></dir></style></legend>

        <small id='f2PdE'></small><noframes id='f2PdE'>

        在常规 UIViewController 上的 UITableViewCell 中的键盘上方滚动 UITextField

        时间:2023-10-03
        <legend id='bWBev'><style id='bWBev'><dir id='bWBev'><q id='bWBev'></q></dir></style></legend>

        <small id='bWBev'></small><noframes id='bWBev'>

      1. <tfoot id='bWBev'></tfoot>
            <tbody id='bWBev'></tbody>

          <i id='bWBev'><tr id='bWBev'><dt id='bWBev'><q id='bWBev'><span id='bWBev'><b id='bWBev'><form id='bWBev'><ins id='bWBev'></ins><ul id='bWBev'></ul><sub id='bWBev'></sub></form><legend id='bWBev'></legend><bdo id='bWBev'><pre id='bWBev'><center id='bWBev'></center></pre></bdo></b><th id='bWBev'></th></span></q></dt></tr></i><div id='bWBev'><tfoot id='bWBev'></tfoot><dl id='bWBev'><fieldset id='bWBev'></fieldset></dl></div>
            <bdo id='bWBev'></bdo><ul id='bWBev'></ul>

                  本文介绍了在常规 UIViewController 上的 UITableViewCell 中的键盘上方滚动 UITextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经尝试过 StackOverflow 上的大部分示例.我也用过苹果的.我似乎遇到的问题是他们没有考虑到 UITextField 在 UITableView 中.我已经多次这样做了,但不是这样.我有一个自定义的 UITableViewCell,里面有一个 UITextField.

                  I have tried most of the examples here on StackOverflow. I also used Apple's. The problem I seem to have with them is that they don't account for the UITextField being in a UITableView. I've done this many times, but not in this way. I have a custom UITableViewCell with a UITextField in it.

                  在我的 UITableView(不是 UITableViewController)上,我需要能够避免将 UITextField 隐藏在 UITableView 下.

                  On my UITableView (which is NOT a UITableViewController), I need to be able to avoid hiding the UITextField under the UITableView.

                  我现在有这个:

                  -(void)viewDidLoad
                  {
                  ....
                      [[NSNotificationCenter defaultCenter] addObserver:self
                                                               selector:@selector(keyboardWillShow:)
                                                                   name:UIKeyboardWillShowNotification
                                                                 object:nil];
                  
                      [[NSNotificationCenter defaultCenter] addObserver:self
                                                               selector:@selector(keyboardWillHide:)
                                                                   name:UIKeyboardWillHideNotification
                                                                 object:nil];
                  
                  ....
                  }
                  
                  - (void)scrollToRectOfTextField {
                      UITableViewCell *cell = (UITableViewCell*)[self.activeTextField superview];
                      CGRect r = CGRectMake(self.activeTextField.frame.origin.x,
                                            cell.frame.origin.y+self.activeTextField.frame.origin.y,
                                            self.activeTextField.frame.size.width,
                                            self.activeTextField.frame.size.height);
                      [self.tableView scrollRectToVisible:r animated:YES];
                  }
                  
                  - (void)keyboardDidShow:(NSNotification *)notification
                  {
                      if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone) {
                          NSDictionary *userInfo = [notification userInfo];
                          CGSize size = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
                          NSLog(@"TableView: %@", NSStringFromCGRect(self.tableView.frame));
                          CGRect newTableViewFrame = CGRectMake(self.tableView.frame.origin.x,
                                                                self.tableView.frame.origin.y,
                                                                self.tableView.frame.size.width, self.tableView.frame.size.height - size.height);
                          self.tableView.frame = newTableViewFrame;
                          NSLog(@"New TableView: %@", NSStringFromCGRect(self.tableView.frame));
                  
                  
                          self.tableView.contentSize = CGSizeMake(self.tableView.contentSize.width, self.tableView.contentSize.height-size.height);
                      }
                  }
                  
                  
                  - (void)keyboardWillHide:(NSNotification *)notification
                  {
                      NSDictionary *userInfo = [notification userInfo];
                      CGSize size = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
                      self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,
                                                        self.tableView.frame.origin.y,
                                                        self.tableView.frame.size.width,
                                                        self.tableView.frame.size.height + size.height);
                  
                      NSLog(@"%@", NSStringFromCGRect(self.tableView.frame));
                  }
                  
                  
                  -(void)textFieldDidBeginEditing
                  {
                       [self scrollToRectOfTextField];
                  }
                  

                  这似乎在我的键盘上推了一堆空白.另请注意,我的 UITextField 上也有一个 inputAccessoryView.

                  This seems to push a bunch of white space up past my keyboard. Please also note that I have an inputAccessoryView on my UITextField as well.

                  哦,我不能使用任何第三方类.我喜欢 TPKeyboardAvoidingScrollView,但我不能使用任何第三方.

                  Oh, and I cannot use any third party classes. I love TPKeyboardAvoidingScrollView, but I cannot use anything third party.

                  推荐答案

                  我花了一整天的时间来解决这个问题.我把它贴在这里,然后找到了一个博客链接和一个非常简单的解决方案.它看起来像这样:

                  I spent all day trying to figure this out. I posted it here, then found a blog link and an incredibly simple solution. It looks like this:

                  -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
                      CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:self.tableView];
                      CGPoint contentOffset = self.tableView.contentOffset;
                  
                      contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);
                  
                      NSLog(@"contentOffset is: %@", NSStringFromCGPoint(contentOffset));
                  
                      [self.tableView setContentOffset:contentOffset animated:YES];
                  
                      return YES;
                  }
                  
                  
                  -(BOOL)textFieldShouldEndEditing:(UITextField *)textField
                  {
                      [textField resignFirstResponder];
                  
                      if ([textField.superview.superview isKindOfClass:[UITableViewCell class]])
                      {
                          UITableViewCell *cell = (UITableViewCell*)textField.superview.superview;
                          NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
                  
                          [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:TRUE];
                      }
                  
                      return YES;
                  }
                  

                  在 iOS 8 中查看此内容

                  这篇关于在常规 UIViewController 上的 UITableViewCell 中的键盘上方滚动 UITextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何检查字体是否在 iOS 版本中可用? 下一篇:在点击文本字段时显示日期选择器

                  相关文章

                  <small id='kjR7n'></small><noframes id='kjR7n'>

                  <legend id='kjR7n'><style id='kjR7n'><dir id='kjR7n'><q id='kjR7n'></q></dir></style></legend>
                • <i id='kjR7n'><tr id='kjR7n'><dt id='kjR7n'><q id='kjR7n'><span id='kjR7n'><b id='kjR7n'><form id='kjR7n'><ins id='kjR7n'></ins><ul id='kjR7n'></ul><sub id='kjR7n'></sub></form><legend id='kjR7n'></legend><bdo id='kjR7n'><pre id='kjR7n'><center id='kjR7n'></center></pre></bdo></b><th id='kjR7n'></th></span></q></dt></tr></i><div id='kjR7n'><tfoot id='kjR7n'></tfoot><dl id='kjR7n'><fieldset id='kjR7n'></fieldset></dl></div>

                    1. <tfoot id='kjR7n'></tfoot>
                      • <bdo id='kjR7n'></bdo><ul id='kjR7n'></ul>