Prevent Keyboard from appearing when tapping on UITextField

Viewed 7905

I'm using a UITextField to show results of a calculation but I don't want the keyboard to appear when the user taps on the UITextField.

I'm using a UITextField because I still want the user to be able to Copy and Paste the calculation back into the UITextField, but I don't want the keyboard to show up.

UIKeyboardWillHide only works after the keyboard is displayed.

9 Answers

Swift 4.2, This works for me. put in 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

For iPads, according to this response of @Awais Jamil, add the following code

textField.inputAssistantItem.leadingBarButtonGroups = []
textField.inputAssistantItem.trailingBarButtonGroups = []
textField.inputView = UIView()

This line of code in your textFieldDidBeginEditing func will do the job.

My func:

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

You can try this as well VIA text field delegate method.

func textFieldDidBeginEditing(_ textField: UITextField) {
        textField.resignFirstResponder()
        // your custom impl
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return false
}
Related