【问题标题】:TextField attach to keyboard while editing - how to make the ScrollView appear same as before editing?TextField 在编辑时附加到键盘 - 如何使 ScrollView 与编辑前显示相同?
【发布时间】:2016-02-19 21:45:10
【问题描述】:

我创建了新的 Xcode 单视图项目。在界面生成器中,我添加了一个 UIScrollView 来覆盖完整的视图。在这个滚动视图上,我添加了一个 UITextField。生成的 UI 如下所示:

请注意,此时滚动视图不会滚动,因为内容仅占用视图的大小并且不会大于它。

现在,为了在编辑时将 UITextField 置于键盘顶部,我按照 Apple 在 Managing The Keyboard 页面上描述的方式进行操作。这样做之后,它给了我预期的行为,在编辑开始时将文本字段带到键盘上方,如下面的屏幕截图所示:

现在,在调用[textfield endEditing:YES] 之后,键盘会自行隐藏,但文本字段不会返回到原来的位置。它返回到略高于其原始位置的位置,现在滚动视图变得可滚动,就好像添加了一点高度一样:

注意上面截图中的滚动条!

我需要帮助在编辑结束后(当键盘隐藏时)恢复视图的原始行为,即 textField 返回到完全相同的位置并且应该发生滚动,因为它在编辑开始之前没有发生。

项目网址:-https://github.com/therohansanap/keyboard.git

【问题讨论】:

  • 您可以创建一个全局 int 来存储您向上推的数量,并在 UIKeyboardWillHideNotification 选择器上使用它来向下推相同的数量

标签: ios objective-c uitextfield uikeyboard


【解决方案1】:
You need adjust scrollview contentOffset textFieldDidBeginEditing and textFieldDidEndEditing.
or 
One controller is available for scrollview auto scroll.

https://github.com/simonbs/BSKeyboardControls

【讨论】:

    【解决方案2】:

    我认为 Apple here 指定的官方方式是保持此功能正常工作的最简单和最佳方式。

    【讨论】:

      【解决方案3】:

      您也可以在不使用键盘通知的情况下执行类似操作。您可能知道我们有 TextField 委托方法,我们可以使用它们来设置 scrollView contentOffset 并获得相同的行为

      - (void)textFieldDidBeginEditing:(UITextField *)textField{
          scrollView.contentOffset = CGPointMake(0, textField.center.y-80); // you can change 80 to whatever which fits your needs
      }
      

      上面的方法设置滚动视图的contentOffset值并且你的textFiled向上移动,而textFieldresignFirstResponder下面的委托方法被调用,你可以在其中设置contentOffset的值

      - (void)textFieldDidEndEditing:(UITextField *)textField{
             scrollView.contentOffset = CGPointMake(0,-80);
      }
      

      注意:您需要将视图中的每个文本字段都设置为您的UIViewController 实例。您还需要您的UIViewController 才能采用UITextFieldDelegate

      【讨论】:

        【解决方案4】:

        查看您的代码,您在尝试定位滚动视图时不需要更改内容插入等。您只需要修改内容偏移属性即可。

        这是修改后的代码:

        @interface ViewController () {
        UITextField *activeField;
        CGPoint scrollViewOldPosition;
        }
        

        修改keyboardWasShow如下:

        // Called when the UIKeyboardDidShowNotification is sent.
         - (void)keyboardWasShown:(NSNotification*)aNotification
        {
            NSDictionary* info = [aNotification userInfo];
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        
        
        CGFloat someSpaceBetweenKeyBoardAndField = 20.0;
        scrollViewOldPosition = self.scrollView.contentOffset;
        
        self.scrollView.contentOffset = CGPointMake(0, kbSize.height - (self.view.frame.size.height - activeField.frame.origin.y - activeField.frame.size.height) + someSpaceBetweenKeyBoardAndField);
        }
        

        键盘将隐藏方法:

        // Called when the UIKeyboardWillHideNotification is sent
        - (void)keyboardWillBeHidden:(NSNotification*)aNotification
        {
            self.scrollView.contentOffset = scrollViewOldPosition;
        }
        

        【讨论】:

          【解决方案5】:

          不是最好的代码,但它有更多你可以使用的功能,任何带有 _ 的东西都是全局变量

          //Handle notification when keyboard appear
          - (void)keyboardOnScreen:(NSNotification *)notification
          {
              if (_isKeyboardShow) {
                  return; //If keyboard is showing then return
              }
              _keyboardHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
              [self animateTextFieldUp: YES];
              _isKeyboardShow = YES; 
          }
          
          //Handle notification when keyboard hide
          - (void)keyboardOffScreen:(NSNotification *)notification
          {
              if(!_isKeyboardShow) return;
              [self animateTextFieldUp: NO];
              _isKeyboardShow = NO;//Missed this line
          }
          
          //Push view up with animation when keyboard show
          - (void) animateTextFieldUp: (BOOL) up
          {
              UITextField *textfield = [UIResponder currentFirstResponder];
              CGPoint windowPoint = [textfield convertPoint:textfield.bounds.origin toView:self.view];
              int movementDistance;
              CGPoint point = [_mainScrollView contentOffset];
          
              //Push up only when blocked by keyboard
              if (windowPoint.y + textfield.frame.size.height >= self.view.frame.size.height - _keyboardHeight) {
                  movementDistance = windowPoint.y - (self.view.frame.size.height - _keyboardHeight) +  textfield.frame.size.height + 10;
                  _oldMovementDistance = movementDistance;
                  int movement = (up ? -movementDistance : movementDistance);
              [_mainScrollView setContentOffset:CGPointMake(0, point.y - movement) animated:YES];
              }
              else { //Push view down the same amount
                  int movement = (up ? -movementDistance : _oldMovementDistance);
                  [_mainScrollView setContentOffset:CGPointMake(0, point.y - movement) animated:YES];
                  _oldMovementDistance = 0;
              }
          }
          

          【讨论】:

          • 我试过这段代码。它第一次工作,但在结束编辑事件后,相同的滚动视图变得可滚动,第二次开始代码的行为完全不像预期的那样。
          • 对不起,我错过了写一行,请再次检查,希望现在可以正常工作! :D
          【解决方案6】:

          在编辑过程中将 UITextField 和 UITextView 移出键盘:

          对于非 UITableViewControllers,将 TPKeyboardAvoidingScrollView.mTPKeyboardAvoidingScrollView.h 源文件拖放到项目中,将 UIScrollView 弹出到视图控制器的 xib 或情节提要中,将滚动视图的类设置为 TPKeyboardAvoidingScrollView,然后将所有控件放入该滚动中看法。您也可以通过编程方式创建它,而无需使用 xib - 只需使用 TPKeyboardAvoidingScrollView 作为您的顶级视图。

          要与 UITableViewController 类一起使用,请将 TPKeyboardAvoidingTableView.m 和 TPKeyboardAvoidingTableView.h 放入您的项目中,并将您的 UITableView 设置为 xib 中的 TPKeyboardAvoidingTableView。如果您没有在控制器中使用 xib,我知道没有简单的方法使其 UITableView 成为自定义类:阻力最小的路径是为其创建 xib。

          您可以从here获得参考。

          希望这会有所帮助。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-04
            • 1970-01-01
            • 2019-05-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多