【问题标题】:iPhone - possible to not show keyboard but still show the cursor in a UITextField?iPhone - 可能不显示键盘但仍然在 UITextField 中显示光标?
【发布时间】:2010-04-04 07:01:43
【问题描述】:

我有一个自定义键盘,我想在用户点击 UITextField 时显示。但同时我想在文本字段中显示光标。如果 canBecomeFirstResponder 返回 NO,则不显示默认键盘,但也不显示光标。

有人可以帮帮我吗?

谢谢。

【问题讨论】:

  • 如果没有键盘,UITextField 中的光标有什么用处?不会太多,这可能就是为什么它不是一个受支持的功能。
  • 拥有自定义键盘。这就是为什么我不想显示默认键盘,但仍然希望光标让​​用户知道他在哪里输入文本
  • 这个肯定有用。雪莉,你找到答案了吗?
  • 检查这篇文章是否对你有用。iphonedevsdk.com/forum/iphone-sdk-development/…

标签: iphone uitextfield


【解决方案1】:

您的问题的答案是为您的自定义键盘创建一个视图,然后编辑您的 UITextFieldinputView 属性并将其传递给您的自定义键盘的视图。

希望对你有帮助。

【讨论】:

    【解决方案2】:

    在 UITextFieldDelegate 中重写以下两个方法。请注意,这种方法对 UITextField 和 UITextView 都有效(在这种情况下,您会覆盖 UITextViewDelegate 中的相应方法)

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        if (!textField.inputView) {
            //hides the keyboard, but still shows the cursor to allow user to view entire text, even if it exceeds the bounds of the textfield
            textField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
        }
        return YES;
    }
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        return NO;
    }
    

    【讨论】:

      【解决方案3】:

      看起来你想要的是一个带有自定义键盘的 UITextField。创建类CustomKeyboard : UIView 并添加按钮/布局视图。然后对于您的文本字段,只需将 inputView 属性设置为 CustomKeyboard textField.inputView = customKeyboard; 类的实例。您还需要将 inputView 属性设置为可读写@property (readwrite, retain) UIView *inputView; 通过设置 inputView 属性,当文本字段成为第一响应者时,标准 iPhone 键盘将不会出现。

      【讨论】:

      • 如果您不需要自定义键盘功能(也许您的文本输入非常不正统),那么创建一个虚拟 UIView(不指定框架)并使 .inputView 可以正常工作。然后在你的 textField 上调用 becomeFirstResponder。
      【解决方案4】:

      注册为键盘通知观察者(例如,在要隐藏键盘的视图控制器中):-

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillShowNotification object:nil];
      

      放入hideKeyboard:function:-

      -(void)hideKeyboard:(NSNotification *)notification {
      
          for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
              for (UIView *keyboard in [keyboardWindow subviews]) {
                  if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {
      
                      keyboard.alpha = 0;
                  }
              }
          }
      }
      

      (感谢 this post 的 luvieere 告诉我如何获得键盘)

      【讨论】:

      • 在 iOS4 中对我不起作用。该方法被调用但没有视图具有 UIKeyboard 前缀
      • thnx 很高兴.. 真的很有帮助,我想在我的文本字段中显示光标闪烁而不显示键盘,它的工作真的 gr8...
      【解决方案5】:

      我不确定这一点,但为什么不直接使用具有相同文本字段内容的 UILabel 并装饰成看起来像带有光标的文本字段。当您需要输入时,将其换成 UITextField。

      【讨论】:

      • 我不想显示默认键盘。以及如何在 UILabel 中显示光标?
      • 啊,你是说你的自定义键盘不是键盘本身,它只是一组按钮,所以你想伪造整个东西?
      【解决方案6】:

      您的问题有 2 个解决方案。 1) 将键盘的 alpha 设置为 0 将使键盘不可见......这可能就是你想要的。光标会出现。

      2) UITextField 实现了 UITextInputTraits 协议。当它成为第一响应者时,它总是会调用键盘。您将需要从它或另一个类继承来更改该默认行为。

      祝你好运。

      如果您告诉我们您想要完成什么,我们可能会建议一种更优雅的方式来完成它。

      玩得开心。

      【讨论】:

        【解决方案7】:

        我看到了两种解决方案 - 要么创建自定义动画(并根据文本字段的第一响应者状态停止或启动它),要么使用 inputView 属性。

        这里是 inputView 方法的解决方案:

        1. UITextFieldinputView 属性设置为空视图并要求它成为第一响应者。这将有效地隐藏默认的inputView(即键盘),但会继续显示闪烁的光标。

        2. 添加 Tap 手势识别器,当用户点击 UITextField 时,将 inputView 属性设置为您的自定义键盘,关闭键盘并要求 UITextField 再次成为第一响应者。

          class BlinkingTextFieldVC: UIViewController {
          
          var blinkingTextField: UITextField!
          
          override func onViewDidLoad() {
              setupView()
          }
          
          func setupView() {
              blinkingTextField = UITextField()
          
              blinkingTextField.inputView = UIView() // empty view will be shown as input method
              blinkingTextField.becomeFirstResponder()
          
              let tapGuesture = UITapGestureRecognizer(target: self, action: #selector(blinkingTextFieldTapped(_:)))
              blinkingTextField.addGestureRecognizer(tapGuesture)
          }
          
          func blinkingTextFieldTapped(_ gesture: UITapGestureRecognizer) {
              if gesture.state == .ended {
                  view.endEditing(true)
                  blinkingTextField.inputView = nil // set your custom input view or nil for default keyboard
                  blinkingTextField.becomeFirstResponder()
              }
          }
          
          }
          

        【讨论】:

          【解决方案8】:

          为什么你甚至需要光标? 我认为您需要做的就是当用户在您自己的键盘上按下某个键时,您可以更新输入的文本值。

          【讨论】:

            猜你喜欢
            • 2014-03-16
            • 2021-09-16
            • 2018-06-17
            • 2020-03-02
            • 1970-01-01
            • 1970-01-01
            • 2012-12-29
            • 1970-01-01
            • 2020-12-31
            相关资源
            最近更新 更多