【问题标题】:How to hide the keyboard clipboard programmatically?如何以编程方式隐藏键盘剪贴板?
【发布时间】:2020-09-09 05:44:26
【问题描述】:

每次我点击 UItextview 这个剪贴板都会出现在底部,我怎样才能以编程方式禁用它?

enter image description here

【问题讨论】:

标签: ios swift uitextfield uitextview


【解决方案1】:

iPhone 上,您只需设置 autocorrectionType = .no 即可完全移除键盘上方的那个栏。

iPad 上,您应该首先添加这些扩展:

extension UITextView {
    func hideSuggestions() {
        // Removes suggestions only
        autocorrectionType = .no
        //Removes Undo, Redo, Copy & Paste options
        removeUndoRedoOptions()
    }
}

extension UITextField {
    func hideSuggestions() {
        // Removes suggestions only
        autocorrectionType = .no
        //Removes Undo, Redo, Copy & Paste options
        removeUndoRedoOptions()
    }
}

extension UIResponder {
    func removeUndoRedoOptions() {
        //Removes Undo, Redo, Copy & Paste options
        inputAssistantItem.leadingBarButtonGroups = []
        inputAssistantItem.trailingBarButtonGroups = []
    }
}

之前:

  1. 要仅删除撤消、重做、复制和粘贴选项,但在键盘顶部显示建议,调用函数 func removeUndoRedoOptions()

  2. 要完全删除建议栏以及撤消、重做、复制和粘贴选项,请调用函数 func hideSuggestions()

【讨论】:

  • 欢迎您,如果它对您有所帮助,如果您选择此作为可能遇到此问题的其他人的可接受答案,我将不胜感激。 :)
  • 嗨,我还有一个问题。我正在使用 unbutton 进行 uitextview 输入,无论如何我可以在编辑 uitextview 时隐藏虚拟键盘?
  • 嗨!是的,当你写完并按下你的 UIButton,你可以调用 myTextField.resignFirstResponder() 来隐藏键盘。此外,如果您不希望在用户点击时出现键盘和/或想要任何其他操作,请设置 myTextField.delegate = self 和 UITextField 委托 func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { switch textField { case myTextField:返回 false 默认值:返回 true } }
  • 谢谢尼克,那么请不要忘记将此答案设置为选中。问候!
【解决方案2】:

您可以禁用 textView 用户交互:

yourTextView.isUserInteractionEnabled = false

或者您可以禁用 textView 的可编辑性

yourTextView.isEditable = false

更新

如果您想让光标出现,但不希望此剪贴板出现,请将您的 textView 设置为第一响应者,并将您的 textView inputView 设置为空白 UIView():

textView.becomeFirstResponder()

之后将你的 textView inputView 设置为空白 UIView():

textView.inputView = UIView()

【讨论】:

  • 我想让光标出现,但不希望这个剪贴板出现。
  • 添加:yourTextView.becomeFirstResponder(),然后添加:yourTextView.inputView = UIView()
  • 嗨,我还有一个问题。我正在使用 unbutton 进行 uitextview 输入,无论如何我可以在编辑 uitextview 时隐藏虚拟键盘?
猜你喜欢
  • 1970-01-01
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-06
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
相关资源
最近更新 更多