代码之家  ›  专栏  ›  技术社区  ›  Swastik

iPhone-可能不显示键盘但仍在UITextField中显示光标?

  •  5
  • Swastik  · 技术社区  · 15 年前

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

    有人能帮帮我吗?

    谢谢。

    8 回复  |  直到 15 年前
        1
  •  6
  •   arturovm    14 年前

    问题的答案是为自定义键盘创建一个视图,然后编辑 inputView 您的财产 UITextField 并将自定义键盘的视图传递给它。

        2
  •  4
  •   Justin    14 年前

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

        3
  •  4
  •   Yogi    10 年前

    重写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;
    }
    
        4
  •  2
  •   Community CDub    8 年前

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

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

    在隐藏的黑板上:功能:-

    -(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 向我演示如何使用键盘)

        5
  •  0
  •   Andiih    15 年前

    我不确定这一点,但为什么不使用一个UILabel,它的内容与文本字段的内容相同,并将其装饰成与文本字段中的光标类似。当你需要输入时,把它换成UITextField。

        6
  •  0
  •   Nathan    14 年前

    1) 将键盘的alpha设置为0将使键盘不可见。。。这可能是你想要的。光标将出现。

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

    祝你好运。

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

    玩得高兴。

        7
  •  0
  •   mra214    6 年前

    以下是inputView方法的解决方案:

    1. 设置 inputView 的属性 UITextField 清空视图并请求它成为第一响应者。这将有效地隐藏违约 输入视图 (即键盘),但将继续显示闪烁的光标。

    2. 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
  •  -1
  •   Edward Ashak    14 年前

    你为什么还需要光标? 我想你需要做的就是当用户在你自己的键盘上按一个键,你就可以更新输入的文本值。