Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keyboard done key action swift iOS doesn't work

I'm new in stackoverflow, I have a problem with new swift code. I have custom the return button on keyboard with "Done", but when I tap on it, don't befall anything... How can I hide the keyboard on tap it? I have added a code (found on this site) for hide the keyboard when you tap somewhere ,not in the keyboard, but I can't custom it with tap on "done" button... Thank you before!!

like image 862
Giorgio Nocera Avatar asked Jul 16 '14 08:07

Giorgio Nocera


People also ask

How do I enable the keyboard in Swift?

To enable SwiftKey and make it your default keyboard on stock Android, head to settings, select Language & input, and choose SwiftKey from the list of options. The keyboard is also available for free in Apple's App Store for iPhone, iPad, and iPod touch devices running iOS 8.

How do you dismiss a keyboard when tapping outside Swift?

Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.


1 Answers

You need to implement delegate method which is called when you hit done button:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {     textField.resignFirstResponder()     return true } 

You also need to conform to UITextFieldDelegate protocol:

// I assume you override UIViewController class. If not add UITextFieldDelegate to your class class MyViewController: UIViewController, UITextFieldDelegate 

The last thing is set up your class to be a text field delegate:

textField.delegate = self 
like image 109
Greg Avatar answered Sep 20 '22 12:09

Greg