Saturday, January 17, 2015

Show/Hide the keyboard while using the UITextView/UITextField

To show the keyboard:


self.inputTextView.becomeFirstResponder()


To hide the keyboard:

self.inputTextView.resignFirstResponder()



To hide the keyboard while tapping outside the UITextField/UITextField:

@IBOutlet weak var inputTextView: UITextView!

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.inputTextView.resignFirstResponder()
    }


To hide the keyboard while tapping the return key in the UITextField:

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var inputTextField: UITextField!
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.inputTextField.delegate = self

1 comment: