iOS | How -To Programmatically Dismiss the UI Keyboard on IPhone or iPad Apps

The IOS app keyboard is a display qwerty keyboard that is automatically displayed when a user places the cursor in an enabled UITextField. Although the keyboard is automatically enabled, it is up to the developer to dismiss the keyboard when the user is finished entering data in a UITextField.

Actually it is quick easy to dismiss the keyboard once you know the basics. The IOS keyboard responds to events from the user. You can make the keyboard move to the background when the “return” button is pressed on the keyboard; you can also make the keyboard dismiss when the user moves the focus to another UITextField or presses a button on the UI. You can also make the keyboard close using finger gestures like a swipe.

In this tutorial, I will demonstrate these three use cases and this should give you all the building blocks to devise other methods of dismissing the keyboard in your own apps.

1- Dismiss Keyboard on UITextField Exist


This first example to demonstrate how to make the keyboard move to the background when a UITextField loses focus. Start by create a new project like a Single-View Application. Make sure the Storyboard and ARC (Automatic Reference Counting) is selected in the Options page. Next open the storyboard and add two TextFields and labels (optional). See Figure 1 below.

Creating Your IOS Project
Creating Your IOS Project

Select the first TextField and open the Attributes inspector. In the TextField properties, you will notice a Keyboard field. Leave the value at Default. You can choose other options like a numerical pad depending on your needs but this example the Default value is fine. In the Return Key field, select the “Done” value and also set the auto-enable the Return Key property. Repeat the sample process for the second TextField. At this point your UI should look something like Figure 2.

Configure the Keyword in the Storyboard
Configure the Keyword in the Storyboard

If you run your app right now and place your cursor in one of the two TexFields you should see the keyboard displayed on screen like in the following Figure 3.

Run the App and Display the Keyboard
Run the App and Display the Keyboard

To make the keyboard dismiss when TextField looses focus you will need to add a Delegate from the TextField to the View Controller Object . That is the yellow globe in the black bar at the bottom of the View Controller. To create a Delegate drag + drop a connection to the globe (Figure 4).

Creating Delegates
Creating Delegates

When you release the mouse button, a popover will appear. Simply select the Delegate option (Figure 5).

Setting the Outlet Delegate in the Dock
Setting the Outlet Delegate in the Dock

Repeat the same process for the second UITextField. Once that is complete, open the Assistant Editor and add two IBOutlets to the ViewController header file one for each UITextField and an IBAction for the button. If you help this operation, read this tutorial on creating Connection and Delegates. Your header field should look like Figure 6.

Adding IBOutlets and IBAction
Adding IBOutlets and IBAction

Now open the View Controller implementation file (.m) and add the following code to dismiss the keyboard:

-(BOOL)textFieldShouldReturn:(UITextField *)textfield{
if(textfield == self.FirstTextField){
[textfield resignFirstResponder];
}else{
[textfield resignFirstResponder];
}
return YES;
}



The textFieldShouldReturn method is defined in the UITextField class. It responds to the delegate that you created earlier and checks to see which UITextField is calling the method and dismisses the keyboard accordingly.

This is nice and simple. When the use click on the “Done” button on the keyboard, the keyboard moves to the background.

2-Dismiss Keyboard Using a Button



This second example builds on the first and basically uses the same code as in the first example. Open the implementation again and copy the following code to the IBAction method we create above:

- (IBAction)DismissButton:(id)sender {
if(self.FirstTextField){
[self.FirstTextField resignFirstResponder];
}else{
[self.SecondTextField resignFirstResponder];
}

}


In the above code, the delegate checks to see which UITextField is active (has the focus) and dismisses the keyboard by call the resignFirstResponder for the appropriate delegate call.

If you run the code and enter some text in one of the field and click on the “Dismiss Keyboard” button, the keyboard will move to the background as before.

3- Use Gestures to Dismiss Keyboard


This third and final example will dismiss the keyboard based on a gesture from the user. To make this work add a Swipe Gesture Recognizer (Figure 7). You will find these under Gesture Recognizers in the Object Library.

Adding a Swipe Gesture Recognizer
Adding a Swipe Gesture Recognizer

To respond to swipes in both directions, you would need two Swipe Gesture Recognizers but for out example we will only use one to detect a downward swipe. First drag and drop a Swipe Gesture Recognizer on to the View Controller. It will automatically add itself to the Scenes Dock (Figure 8).

Swipe Object in the Dock
Swipe Object in the Dock

Open the header file using the Assistant Editor and drag a connection from the Swipe to the header file (Figure 9). In the Popover change the Connection to Action, prove a name for your method and change the type to UISwipeGestureRecognizer. Click “Connect” create the method and enable the connection (Figure 9).

Adding IBAction for Swipe
Adding IBAction for Swipe
Configure the Swipe IBAction
Configure the Swipe IBAction

One more step in our Swipe configuration. Select the Swipe in the Dock and switch to the Attributes assistant and select the type of swipe to perform. You can select “Up”, “Down”, “Left” or “Right”. For this example, select “Down”. Also leave the Touches at 1. This determines the number of fingers to use for your swiping (Figure 10).

Configure the Swipe Gesture Recognizer
Configure the Swipe Gesture Recognizer

Now add a Delegate for our Swipe. Open the storyboard again and drag a connection to the View Controller Object in the Dock. Choose the method you just create in the Outlet popover. Now the Swipe will respond to your finger movements (Figure 11).

Add the Delegate for the Swipe Gesture Recognizer Action
Add the Delegate for the Swipe Gesture Recognizer Action

All is needed now to add some code to your new method. In this example, my method is called closeKeyboard and here is the code for it:

- (IBAction)CloseKeyboard:(id)sender {
if(self.FirstTextField){
[self.FirstTextField resignFirstResponder];
}else{
[self.SecondTextField resignFirstResponder];
}
}

If you run your app again, use the mouse pointer and drag the pointer downwards and release the mouse button. The keyboard will dismiss.

This covers most of the use cases for dismissing the keyboard. You can implement and adapt this code in this tutorial to fit your needs. Also build on these examples to build use the keyboard in other ways.

More by this Author


Comments 2 comments

keithatbbpp 4 years ago from Alberta, Canada

Hi Kevin,

I cannot get the keyboard to dismiss with the button after entering text in the second textfield, it does dismiss with the button after entering text in the first textfield. Seems like the else statement would take care of that in the "(IBAction)DismissButton" but it does not.

Keith


klanguedoc profile image

klanguedoc 4 years ago from Canada Author

Hi Keith. Heads up to you. There is a bug in my code...so sorry. The code doesn't check to see which field has focus so the first condition is always true. I will work out a fix.

Sorry about that.

Kevin

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working