Dismiss Keyboard

suggest change

Swift

Ctrl + Drag from the UItextfield in MainStoryboard to the ViewController Class and create a UITextField Outlet

After that select the UItextField again and Ctrl+drag in ViewController class but this time select Action connection and on storage select Did End On Exit then click connect.

in the action you just created type the name of your UItextField .resignFirstResponder()

@IBAction func textFieldResign(sender: AnyObject) {
     yourTextFieldName.resignFirstResponder()
 }

This will take care of hiding the keyboard when pressing the return key on keyboard.

Another example of hiding the keyboard when return key is pressed:

we add UITextFieldDelegate protocol next to UIViewController

in the vieDidLoad function we add self.yourTextFieldName.delegate = self

And Finally we add this

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

The final code is this:

class ViewController: UIViewController, UITextFieldDelegate  {

@IBOutlet var textField: UITextField!

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

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
    view.endEditing(true)
    super.touchesBegan(touches, withEvent: event)
}
override func viewDidLoad() {
    super.viewDidLoad()
    self.textField.delegate = self
        }

}

Objective-C

[textField resignFirstResponder];

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


UITextField:
* Dismiss Keyboard

Table Of Contents
12 UIView
15 UIColor
26 UIImage
28 CALayer
30 NSDate
37 UITextField
40 iBeacon
49 NSTimer
79 NSURL
87 AWS SDK
96 NSData
101 Segues
104 EventKit
105 NSBundle
106 SiriKit
111 StoreKit
117 3D Touch
119 Keychain
122 Block
141 AirDrop
144 UISlider
145 Carthage
146 HealthKit
151 plist
157 MVVM
164 UIPhoenix
166 Simulator
168 NSArray
169 OpenGL
175 Core Data
179 MyLayout
180 UIFont
189 Security
200 Codable