Reputation: 684
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.
Upvotes: 8
Views: 11107
Reputation: 469
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
Upvotes: 1
Reputation: 1252
You can try this as well VIA text field delegate method.
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.resignFirstResponder()
// your custom impl
}
Upvotes: 0
Reputation: 1873
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()
}
Upvotes: 3
Reputation: 186
For iPads, according to this response of @Awais Jamil, add the following code
textField.inputAssistantItem.leadingBarButtonGroups = []
textField.inputAssistantItem.trailingBarButtonGroups = []
Upvotes: 5
Reputation: 406
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
Upvotes: 12
Reputation: 798
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
}
Upvotes: 3
Reputation:
You can hide keyboard in UITextFieldDelegate
method textFieldDidBeginEditing:(textField: UITextField)
like below :
func textFieldDidBeginEditing:(textField: UITextField) {
self.view.endEditing(true)
}
Upvotes: 2
Reputation: 4425
Its quite simple to do with UITextField. Use this code in viewDidLoad()
self.txtresult.inputView = UIView()
self.txtresult.inputAccessoryView = UIView()
Upvotes: 8
Reputation: 14030
you could use an "empty" inputview like this:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
let inputView = UIView(frame: .zero)
inputView.backgroundColor = UIColor.clearColor()
inputView.opaque = false
textField.inputView = inputView
return true
}
Upvotes: 1