- Add
UITextFieldDelegate
to the view controller. - In order to enable the return key function, you should implement the
textFieldShouldReturn
function and set the delegate of the text field to self. - In order to close the keyboard when users touch the screen, you can override the
touchesBegan
function
Fail to dismiss keyboard in scroll views
Inside a scroll view, touchesBegan
function does not work. In order to solve this, one simple way is to go to the storyboard, select the scroll view. In the inspector, find keyboard attribute, and set it to Dismiss on drag
. Although it is not as smooth as touches, it can temporarily solve the problem.
Sample code
1 | class exampleViewController: UIViewController, UITextFieldDelegate { |