How to Dismiss UITextField’s Keyboard in your Swift App

This is the way I usually do it.

You should add below code into viewcontroller which you want to use.

We are going to override the method touchesBegan:withEvent: for the ViewController itself.

This is called when the view sees a touch event (as it begins, appropriately).

In it, we are simply going to tell our main view to end editing, like so:

 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
    view.endEditing(true)
    super.touchesBegan(touches, withEvent: event)
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: