在不打开键盘的情况下点击文本字段时打开视图

Open view when tapping the text field without opening the keyboard

我正在开发一个应用程序,当用户单击文本字段但我不想打开键盘时,我想在其中打开一个视图。我怎样才能做到这一点?

使用

将您的 ViewController 设置为您的 TextFields 委托
self.yourTextfield.delegate = self

现在实现 UITextFieldDelegate,如下所示

extension ViewController : UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool   {
        //Load your VC here
        return false
    }
}

就这些:)希望对您有所帮助

您可以通过将空 custom input view 设置为 textField:

来轻松实现它
textField.inputView = UIView(frame: CGRect.zero)

然后 textField 可以正常成为第一响应者,但是将显示此视图(带有 zero 框架)而不是键盘。

您只需将 点击手势识别器 添加到文本字段即可。

let tapGestRec = UITapGestureRecognizer(target: self, action: #selector(textFieldTapped))
textField.addGestureRecognizer(tapGestRec)

您可以使用下面的textField Delegate方法

func textFieldDidBeginEditing(_ textField: UITextField) {
        yourTextField.resignFirstResponder()
        //code for show view
    }

希望对您有所帮助!