I'm wondering how to make the keyboard disappear when the user touches outside of a UITextField
.
IOS – Dismiss keyboard when touching outside of UITextField
cocoa-touchiosuikituitextfield
Related Question
- Ios – How to sort an NSMutableArray with custom objects in it
- Ios – Vertically align text to top within a UILabel
- Ios – How to make a UITextField move up when the keyboard is present – on starting to edit
- Ios – Xcode – How to fix ‘NSUnknownKeyException’, reason: … this class is not key value coding-compliant for the key X” error
- Ios – Passing data between view controllers
- Ios – Programmatically change UITextField Keyboard type
- Ios – Close iOS Keyboard by touching anywhere using Swift
- Ios – How to dismiss keyboard when touching anywhere outside UITextField (in swift)
Best Solution
You'll need to add an
UITapGestureRecogniser
and assign it to the view, and then call resign first responder on theUITextField
on it's selector.The code:
In viewDidLoad
In dismissKeyboard:
(Where
aTextField
is the textfield that is responsible for the keyboard)Swift 3 version looks like that
For dismissKeyboard