Removing the Virtual Keyboard in Swift

if you’re creating an iOS app that requires typing text, your app will need to display the iOS virtual keyboard that slides up from the bottom of the screen. The problem with the virtual keyboard is that although it pops up automatically, you have to write code to make it go away.

There are basically four steps you need to follow to write code that makes the virtual keyboard go away. First, you’ll likely need a text field to allow the user to type text on the virtual keyboard, so you need to add the UITextFieldDelete to your class file like this in Swift:

import UIKit

//  1. Add UITextFieldDelegate to the ViewController class

class ViewController: UIViewController, UITextFieldDelegate {

Second, you’ll need to set the text field to the UITextFieldDelegate. You can do through the Connections Inspector on Xcode or just use code like this in Swift:

        //  2. Set text field to delegate
        textFieldEntry.delegate = self

Third, you need to create a function called textFieldShouldReturn to end editing. This function hides the virtual keyboard if the user taps the Return key:

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return true
    }

Fourth, you need to create a second function called touchesBegan to tell the text field to let the virtual keyboard disappear if the user taps anywhere away from the text field:

   //  4. Create a touchesBegan function to dismiss keyboard if the user taps outside of the text field or keyboard
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        textFieldEntry.resignFirstResponder()
    }

So if you create a simple iOS, single view app in Xcode using Swift, just place a text field on the view (user interface) and link it to your view controller file as an IBOutlet such as naming the text field “textFieldEntry”. Then your entire Swift class would look like this:

//  ViewController.swift
//  KeyboardRemoval
//

import UIKit

//  1. Add UITextFieldDelegate to the ViewController class

class ViewController: UIViewController, UITextFieldDelegate {
   
    @IBOutlet weak var textFieldEntry: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        //  2. Set text field to delegate
        textFieldEntry.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    //  3. Create a textFieldShouldReturn function that will dismiss the keyboard when the user is done editing the text field
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return true
    }

    //  4. Create a touchesBegan function to dismiss keyboard if the user taps outside of the text field or keyboard
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        textFieldEntry.resignFirstResponder()
    }

}

Now if the user taps the Return key on the virtual keyboard or taps away from the text field and the virtual keyboard, the virtual keyboard will tuck itself out of sight.

April 29th, 2016 by
HTML Snippets Powered By : XYZScripts.com