I put a numeric keypad in my app for inputing numbers into a text view, but in order to input numbers I have to click on the text view. Once I do so, the regular keyboard co
I used the keyboardWillShow Notification
and textField.endEditing(true)
:
lazy var myTextField: UITextField = {
let textField = UITextField()
// ....
return textField
}()
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}
@objc func keyboardWillShow(_ notification: Notification) {
myTextField.endEditing(true)
// if using a textView >>> myTextView.endEditing(true) <<<
}
In Xcode 8.2 you can do it easily by unchecking state "enabled" option.
Or if you want to do it via code. You can simply create an @IBOutlet of this text field, give it a name and then use this variable name in the viewDidLoad func (or any custom one if you intent to) like this (in swift 3.0.1):
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myTextField.isEditable = false
}
If it's a UITextField, you can set it's enabled
property to NO.
If it's a UITextView, you can implement -textViewShouldBeginEditing:
in its delegate to return NO, so that it'll never start editing. Or you can subclass it and override -canBecomeFirstResponder
to return NO. Or you could take advantage of its editing behavior and put your numeric buttons into a view which you use as the text view's inputView
. This is supposed to cause the buttons to be displayed when the text view is edited. That may or may not be what you want.
private void TxtExpiry_EditingDidBegin(object sender, EventArgs e)
{
((UITextField)sender).ResignFirstResponder();
}
In C# this worked for me, I don't use the storyboard.
Depending on how you have your existing buttons working this could break them, but you could prevent the keyboard from showing up setting the textView's editable property to NO
myTextView.editable = NO