【问题标题】:How to manually send UIKeyboard notification如何手动发送 UIKeyboard 通知
【发布时间】:2013-05-10 11:24:25
【问题描述】:

我的 ui 中有几个 uitextfields,其中键盘会与屏幕底部的几个 uitextfields 重叠,为了处理这个问题,我已经实现了 UIKeyboard 通知,例如

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

我的 uitextfields 键盘 returnKeyType 是 UIReturnKeyNext 所以当用户点击 Next 按钮时,我会将我的下一个文本字段设置为 becomeFirstResponder 但是当被隐藏的文本字段键盘成为第一响应者,它的框架没有改变并更新到 uikeyboard 之外的可见区域边界,因为通知仅在我第一次点击文本字段时触发。 我需要在每个 becomeFirstResponder 事件上触发这个 keyboardWasShown 方法。 提前感谢任何帮助。

【问题讨论】:

    标签: iphone ios objective-c xcode ipad


    【解决方案1】:

    首先确保您在这两种方法中的代码是正确的,并考虑到当您将第一响应者从 UITextField 更改为另一个时,将会有两个通知发送,一个用于隐藏键盘,一个用于显示键盘。

    我不知道您如何处理此通知以及您是否有权访问文本字段,但我设法仅使用 UITextFields 的委托方法为某些文本文件实现了 scrolling 行为

    -(BOOL)textFieldShouldReturn:(UITextField *)textField(将内容向下移动,因为键盘已隐藏)

    -(void)textFieldDidBeginEditing:(UITextField *)textField(上移内容,因为键盘会出现)

    即使您的文本字段已设置为 inputView,您也可以使用此方法处理内容移动,例如 UIPickerView。

    【讨论】:

    • 将视图的框架更改为键盘的框架
    【解决方案2】:

    我建议使用插入式替换库TPKeyboardAvoiding,它将处理视图的所有移动。如果,由于屏幕键盘,文本字段将被隐藏。它非常易于使用,而且效果很好。

    【讨论】:

      【解决方案3】:

      这就是我实现代码的方式:

      1) 我的视图是 UIScrollView。

      2) 我的视图中有多个 UITextField 并使用标签 ID 将它们区分开来。当用户点击 UITextField 时,会调用此委托来移动 UIScrollView:

      - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
      
      switch (textField.tag)
      {
          case 0:
              [myScrollView setContentOffset:CGPointMake(0, 18) animated:YES];
              break;
          case 1:
              [myScrollView setContentOffset:CGPointMake(0, 56) animated:YES];
              break;
          case 2:
              [myScrollView setContentOffset:CGPointMake(0, 94) animated:YES];
              break;
          case 3:
              [myScrollView setContentOffset:CGPointMake(0, 132) animated:YES];
              break;
          case 4:
              [myScrollView setContentOffset:CGPointMake(0, 170) animated:YES];
              break;
          case 5:
              [myScrollView setContentOffset:CGPointMake(0, 208) animated:YES];
              break;
          case 6:
              [myScrollView setContentOffset:CGPointMake(0, 246) animated:YES];
              break;
          case 7:
              [myScrollView setContentOffset:CGPointMake(0, 284) animated:YES];
              break;
          case 8:
              [myScrollView setContentOffset:CGPointMake(0, 322) animated:YES];
              break;
          default:
              break;
      }
      return YES;
      }
      

      3)我也有UITextView,用这个delegate达到同样的效果:

      - (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
      
      if(textView.tag == 0)
      {
          [myScrollView setContentOffset:CGPointMake(0, 360) animated:YES];
          // do some stuff...
      }
      if(textView.tag == 1)
      {
          [myScrollView setContentOffset:CGPointMake(0, 504) animated:YES];
          // do some stuff...
      }
      return YES;
      }
      

      注意:CGPointMake(0, 123) 值取决于您自己的文本字段坐标。

      【讨论】:

        猜你喜欢
        • 2021-05-16
        • 2012-02-22
        • 2021-09-24
        • 1970-01-01
        • 2021-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多