Swift 4 - 如何使用按钮启用/禁用键盘

Swift 4 - How to enable / disable keyboard by using buttons

我的棘手问题是:

我有 2 个按钮和 2 个 UITextView。 如您所知,当我按下 UITextView 时,会出现键盘。嗯,这不是我想要的。

我想根据录制的特定@IBAction 启用/禁用显示器的键盘。

场景如下: - 第一个按钮(键盘图标)允许用户显示键盘以在其中一个 UITextView 中键入内容,并在顶部使用 FirstResponder init。

我已经知道有 :

isUserInteractionEnabled

textViewDidBeginEditing

但它不太合适and/or解决我的问题。

这里有一个更明确的屏幕(不要在意第三个绿色验证按钮,它只是用于 .POST 功能):

感谢您的帮助!

单击按钮时关闭键盘的功能:

@IBAction func hideKeyboard() {
     textView.resignFirstResponder()
 }

显示键盘的函数:

 @IBAction func showKeyboard() {
     textView.becomeFirstResponder()
 }

像这样的东西应该可以工作:

class MyViewController: UIViewController, UITextViewDelegate {

    @IBOutlet var yourTextView: UITextView

        override func viewDidLoad() {
            super.viewDidLoad()

            yourTextView.delegate = self
        }

        @IBAction func showKeyboard(_ sender: UIButton) {
        self.yourTextView.becomeFirstResponder()

    }

      @IBAction func hideKeyboard(_ sender: UIButton) {
        self.yourTextView.resignFirstResponder()

    }

    }

你说

But it's not really working in my case.

为什么?

您可以禁用与 (swift 3) 的文本字段交互:

textField.isUserInteractionEnabled = false

接下来,当您单击键盘按钮时,您可以重新启用交互,这样当用户单击其中一个文本字段时,键盘就会打开。

在键盘关闭上(您可以看到 UIKeyboardWillHideNotification 回调通知)您可以将交互设置为 false。

如果我正确理解你的问题,你不希望键盘在用户点击 textField 时出现,而应该只在用户点击键盘按钮时出现,并且应该在点击其他按钮时关闭。

所有发布的答案大多只关注显示键盘并在点击按钮时关闭它们的第二部分。更棘手的是当用户点击 textField 时阻止键盘出现 :)

您可以尝试的可能解决方案及其缺点:

解决方案一:

尝试设置文本字段 isEnabled = false 确定这将阻止用户点击文本字段时出现键盘,但猜猜是什么键盘即使在调用时也不会出现 textfield.becomeFirstResponder() 啊麻烦 :)

方案二:

实现 textFieldShouldBeginEditing UITextField 委托并根据是否使用点击按钮或文本字段本身返回 true 或 false。

当然可以,但您需要想办法告诉 textFieldShouldBeginEditing 为什么它是因为按钮或再次触摸 textField 并发症而触发的。

我的解决方案:

使用 2 个文本字段。一个永远禁用用户交互并使用另一个永远不会向用户显示但会在需要时显示键盘的文本字段。

话不多说让代码:)

第 1 步:

假设出现在屏幕上的 textField 名为 textField

textfield.isEnabled = false

这将确保无论您做什么键盘都不会出现在该键盘上。

第 2 步:

创建零帧的临时文本字段(用户永远不能点击 :P )

    tempTextField = UITextField(frame: CGRect.zero)
    tempTextField.delegate = self
    self.view.addSubview(tempTextField)

第 3 步:

现在,当用户点击显示键盘按钮时,让您的临时文本字段成为第一响应者

tempTextField.becomeFirstResponder()

并且当用户点击 tempTextField 的其他按钮 resignFirstResponder 时。

tempTextField.resignFirstResponder()

第 4 步:

但是当用户键入时,我的文本字段上什么也没有出现,请稍候。等待简单实施

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField == tempTextField {
        self.textfield.text = (self.textfield.text ?? "") + string
    }
    return true
}

编辑:

您实际上并不需要两个文本字段,您也可以使用 UILabelUITextField 来实现相同的目的。我选择了 UITextField,因为我不确定你的其他要求是什么!

问题已解决。希望对您有所帮助:)

只需复制并粘贴嵌入键盘的配件按钮的简单代码

 func addKeyboardToolbar()  {
    let ViewForDoneButtonOnKeyboard = UIToolbar()
    ViewForDoneButtonOnKeyboard.sizeToFit()
    let button = UIButton.init(type: .custom)
    button.setImage(UIImage.init(named: "login-logo"), for: UIControlState.normal)
    button.addTarget(self, action:#selector(doneBtnfromKeyboardClicked), for:.touchUpInside)
    button.frame = CGRect.init(x: 0, y: 0, width:UIScreen.main.bounds.width, height: 30) //CGRectMake(0, 0, 30, 30)
    let barButton = UIBarButtonItem.init(customView: button)
    ViewForDoneButtonOnKeyboard.items = [barButton]
    postTextView.inputAccessoryView = ViewForDoneButtonOnKeyboard
}


@IBAction func doneBtnfromKeyboardClicked (sender: Any) {
        self.contentView.endEditing(true)
    }