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: