Saturday, May 16, 2020

Making the keyboard disappear

When you make an iOS app (Swift/Xcode) that has a text field for the user to type into, you must make sure you test with the iOS keyboard (the software keyboard that pops up). In the Simulator program, select

Hardware –> Keyboard –> Connect Hardware Keyboard

and make sure it is turned OFF (no check mark).

Hardware Keyboard means the big keyboard on your computer.No Hardware Keyboard means use the little keyboard that pops up on the phone.

The Hardware Keyboard is good for testing that your app can read the contents of the text field. But you also need to test the UI (user interface) to make sure that it allows the user to do everything they want.

This video demonstrates the problem: https://youtu.be/TQjzu8y1F_s

You can use code like this to help fix it:
class WordsViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(
            target: self, action: "dismissKeyboard")
        view.addGestureRecognizer(tap)
    }
    
    @objc func dismissKeyboard() {
        // Causes the view (or one of its embedded text fields) 
        // to resign the first responder status.
        view.endEditing(true)
    }
}


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.