Say you are using a UITextField, you want to tap another place to hide the keyboard. This article provides two methods to implement the simple functionality.

First, let’s realize it mainly through IB:



Make it an action, add only one line of code as follow:


All done, so easy, right?


Then, let’s realize it through pure code:

You just need to add two piece of code:

Add image in viewDidLoad:

and add a method image

The effect is exactly the same as before. While there are of course other ways to hide keyboard, but that’s not the point in this article.

IB or pure code, which one do you prefer?

The demo project is here.