如何以编程方式隐藏键盘剪贴板?

How to hide the keyboard clipboard programmatically?

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

enter image description here

您可以禁用 textView 用户交互:

yourTextView.isUserInteractionEnabled = false

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

yourTextView.isEditable = false

更新

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

textView.becomeFirstResponder()

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

textView.inputView = UIView()

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