【问题标题】:Prevent Keyboard from appearing when tapping on UITextField点击 UITextField 时防止键盘出现
【发布时间】:2018-01-15 13:42:18
【问题描述】:

我正在使用 UITextField 来显示计算结果,但我希望在用户点击 UITextField。

我正在使用 UITextField,因为我仍然希望用户能够将计算复制并粘贴回 UITextField,但我不希望键盘出现。

UIKeyboardWillHide 仅在键盘显示后有效。

【问题讨论】:

  • 您可以在编辑模式禁用的情况下使用 UITextView。
  • 我使用 textField.isUserInteractionEnabled = false 但是复制和粘贴也不再起作用。
  • 如果禁用 userInteraction 意味着您无法检测到 UITextField 上的点击。建议使用 UITextView 并禁用编辑模式。
  • 在 Swift 3 中我看不到 UITextField.enabled = false ?
  • 不是 UITextField,使用 UITextView 和代码:textView.isEditable = false

标签: ios swift keyboard uitextfield


【解决方案1】:

Swift 4.2,这对我有用。 输入viewDidLoad()

//It will Hide Keyboard
textField.inputView = UIView()
//It will Hide Keyboard tool bar
textField.inputAccessoryView = UIView()
//It will Hide the cursor
textField.tintColor = .white

【讨论】:

【解决方案2】:

使用 UITextField 非常简单。在 viewDidLoad()

中使用此代码
self.txtresult.inputView = UIView()
  self.txtresult.inputAccessoryView = UIView()

【讨论】:

    【解决方案3】:

    首先在self 类中将delegate 与您的UITextField 设置在一起。

    您可以使用以下 2 种方式。
    1. 来自故事板
    2.来自代码(你可以写在viewDidLoad()

    textField.delegate = self

    然后在你的类中声明协议UITextFieldDelegate

    现在调用委托方法。

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return true
    }
    

    【讨论】:

      【解决方案4】:
      textField.inputView = UIView()
      

      你的 textFieldDidBeginEditing 函数中的这行代码将完成这项工作。

      我的功能:

      func textFieldDidBeginEditing(_ textField: UITextField) {
              keyboardView.activeTextField = textField
              textField.inputView = UIView()
          }
      

      【讨论】:

        【解决方案5】:

        您可以在UITextFieldDelegate 方法textFieldDidBeginEditing:(textField: UITextField) 中隐藏键盘,如下所示:

        func textFieldDidBeginEditing:(textField: UITextField) {
            self.view.endEditing(true)
        }
        

        【讨论】:

          【解决方案6】:

          您可以像这样使用“空”输入视图:

          func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
              let inputView = UIView(frame: .zero)
              inputView.backgroundColor = UIColor.clearColor()
              inputView.opaque = false
              textField.inputView = inputView
          
              return true
          }
          

          【讨论】:

            【解决方案7】:

            对于iPad,根据@Awais Jamil的这个response,添加以下代码

            textField.inputAssistantItem.leadingBarButtonGroups = []
            textField.inputAssistantItem.trailingBarButtonGroups = []
            

            【讨论】:

              【解决方案8】:

              你也可以试试这个 VIA 文本字段委托方法。

              func textFieldDidBeginEditing(_ textField: UITextField) {
                      textField.resignFirstResponder()
                      // your custom impl
              }
              

              【讨论】:

                猜你喜欢
                • 2019-06-13
                • 1970-01-01
                • 2020-05-22
                • 1970-01-01
                • 2017-08-09
                • 1970-01-01
                • 2011-11-03
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多