I want to hide the keyboard after clicking the return button, and I have searched on website and wrote the same code as it shows on the internet. However, the keyboard still didn't hide. I don't know why?
-
1Please do not post code as picture. Copy and paste the text in here. – Code Different Mar 02 '16 at 01:51
-
The first question is: Have you set your `UITextField` delegate? – paulvs Mar 02 '16 at 02:27
-
Possible duplicate of [How to hide keyboard in swift on pressing return key?](http://stackoverflow.com/questions/24180954/how-to-hide-keyboard-in-swift-on-pressing-return-key) – mattsap Mar 02 '16 at 02:44
2 Answers
I think like @paulvs said is to set the delegate
so drag the text field on the storyboard to the "donut" at the top of the screen and select delegate
then create and IBOutlet for the text field by being on the storyboard and then tapping the assistant editor...it looks like two overlapping circles Control drag the text field to your viewcontroller and make sure it is on Outlet and then name it textField
Next set the delegate by setting it equal to itself so
textField.delegate = self
then create a delegate function (should auto fill as you type) but it is func textfieldshouldreturn
inside that func do this
textField.resignfirstresponder()
then return true

- 2,330
- 5
- 38
- 70
-
found this too http://stackoverflow.com/questions/24908966/hide-keyboard-for-text-field-in-swift-programming-language – RubberDucky4444 Mar 02 '16 at 02:44
try to confirm: 1. inherit from UITextFieldDelegate; 2. set the current view controller self as delegate; 3. return false at the overrided func textfieldshouldreturn that you called.

- 168
- 2
- 13