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.
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
Its quite simple to do with UITextField. Use this code in viewDidLoad()
self.txtresult.inputView = UIView()
self.txtresult.inputAccessoryView = UIView()
First set delegate
with your UITextField
in self
class.
You can do with below 2 ways.
1. From storyboard
2. From Code ( You can write at viewDidLoad()
) textField.delegate = self
Then declare protocol UITextFieldDelegate
in your class.
Now call delegate method.
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return true
}
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()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With