【问题标题】:Swift 4 - How to enable / disable keyboard by using buttonsSwift 4 - 如何使用按钮启用/禁用键盘
【发布时间】:2017-11-16 16:52:09
【问题描述】:

我的棘手问题是:

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

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

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

  • 第二个按钮(REC 图标)允许用户说话并在预选的 TextView 中显示,但不显示键盘

我已经知道有:

isUserInteractionEnabled

textViewDidBeginEditing

但它不太适合和/或解决我的问题。

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

感谢您的帮助!

【问题讨论】:

  • 你的意思是当你点击其他按钮时关闭键盘?
  • 考虑到 UITextView 之一被选中。是的,这绝对是我想要的。我想在按下特定按钮时关闭键盘,并在按下另一个按钮时显示它。
  • @danyl :请查看我发布的详细答案,您可以尝试多种解决方案,最后一个可以正常工作:D

标签: ios swift xcode ibaction


【解决方案1】:

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

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

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

解决方案 1:

尝试设置 textfield isEnabled = false 确保当用户点击 textField 时这将阻止键盘出现,但猜猜即使在调用 textfield.becomeFirstResponder() 时键盘也不会出现@ 啊哈麻烦 :)

解决方案 2:

实现 UITextField 委托的 textFieldShouldBeginEditing 并根据是点击按钮还是 textField 本身返回 true 或 false。

当然可以,但是你需要想办法告诉textFieldShouldBeginEditing为什么它是由于按钮或再次触摸 textField 的复杂性而触发的。

我的解决方案:

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

说得够多了,让我们写代码吧 :)

第 1 步:

假设您出现在屏幕上的 textField 称为 textField

textfield.isEnabled = false

这将确保您所做的任何键盘都不会出现在此键盘上。

第 2 步:

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

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

第 3 步:

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

tempTextField.becomeFirstResponder()

当用户点击其他按钮 resignFirstResponder 时,用于 tempTextField。

tempTextField.resignFirstResponder()

第 4 步:

但是,当用户键入时,我的 textField 上没有显示任何内容。等待简单实现

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 是因为我不确定您的其他要求是什么!

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

【讨论】:

  • @danyl : 很高兴我能帮上忙 :) 编码愉快 :)
【解决方案2】:

这样的事情应该可以工作:

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()

    }

    }

【讨论】:

    【解决方案3】:

    你说

    但在我的情况下它并没有真正起作用。

    为什么?

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

    textField.isUserInteractionEnabled = false
    

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

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

    【讨论】:

      【解决方案4】:

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

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

      显示键盘的功能:

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

      【讨论】:

      • 他有两个按钮。你说的就是我的意思@rmaddy :)
      【解决方案5】:

      只需复制并粘贴简单的代码即可嵌入带有键盘的附件按钮

       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)
          }
      

      【讨论】:

      • 感谢您的回答。 2年后,我已经找到了解决方案:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多