I\'d like to know the simplest code to dismiss the number pad keyboard when tapping anywhere outside the number pad. It\'s a simple application to input a number inside a te
For swift 3/4 I like this case:
yourPhonePadField.delegate = self
And override this function:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
For Swift, the easiest for dismissing keypad for all text fields by tapping anywhere outside the text field is:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
{
self.view.endEditing(true)
}
You can implement @mbm's answer really nicely by putting a didSet
on the outlet and attaching your inputAccessoryView there:
Swift code:
@IBOutlet var input: UITextField! { didSet {
let toolbar = UIToolbar(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 0, height: 44)))
toolbar.items = [
UIBarButtonItem(barButtonSystemItem: .done, target: self.input,
action: #selector(resignFirstResponder))
]
input.inputAccessoryView = toolbar
}}
In xcode 8 / iOS 10 It ends up looking something like this: