Today, I will look at hiding the soft keyboard from the screen when entering information into a text field. It’s not a problem if you have a hardware keyboard which is the default for the Xcode simulator on the Mac or when you run on a device with a Bluetooth keyboard as the soft keyboard does not show.
The issue is that once you are in a UITextField, the keyboard does not get dismissed by default and takes up the bottom section of the viewing area which quite often hides other controls. When I have finished with the form and select “Return” I expect the keyboard to vanish until I enter another UITextField or re-enter the same one.
I am an infrequent IOS developer. Sometimes, it can be years between writing anything. I was using Objective C in iOS 6 and 7 then 9 and have since switched to Swift in iOS 11. I often forget some bits that are not obvious but should be easy, so when I come across something like that I will add it to this blog as an example.
The following example here uses Swift 4 in Xcode 9 on iOS 11. This article will act as a reminder to myself and can also help others who are new to this situation or find the resignFirstResponder method of the UITextField control is not working.
What I will do is create some UITextField controls on a View Controller with a UIButton lower down that will be hidden from the keyboard. After adding some labels and position constraints, I am ready to continue.
Outlets will be added for the UITextField controls and if this was a real application we would add an IBAction for the UIButton but it is not and I want to keep the example very simple.
@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var firstName: UITextField!
Next, we make the View Controller a delegate for a UITextField control. This is done by appending “, UITextFieldDelegate” to the class.
Then we have to make sure each of our UITextField controls actually delegate to the View Controller by adding some code to the viewDidLoad() function after super.viewDidLoad().
self.firstName.delegate = self
self.lastName.delegate = self
self.yourAge.delegate = self
Finally add a function to dismiss the keyboard for any UITextField control that you have delegated to the View Controller.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
Run on an actual device or in the Xcode simulator (cmd+K to toggle the soft keyboard) and after entry select the “Return” key on the soft keyboard to dismiss it.
The full code of the View Controller is shown below.
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.firstName.delegate = self
self.lastName.delegate = self
self.yearsOld.delegate = self
}
@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var yearsOld: UITextField!
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}