iosswiftinputview

How to switch custom in-app keyboards


Recently I learned to make custom in-app keyboards. Now I want to be able to swap between multiple custom keyboards. However, resetting the textField.inputView property does not seem to work.

I recreated a simplified version of this problem in the following project. The UIViews represent actual custom keyboards.

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        let blueInputView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
        blueInputView.backgroundColor = UIColor.blueColor()

        textField.inputView = blueInputView
        textField.becomeFirstResponder()


    }

    @IBAction func changeInputViewButtonTapped(sender: UIButton) {

        let yellowInputView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
        yellowInputView.backgroundColor = UIColor.yellowColor()

        // this doesn't cause the view to switch
        textField.inputView = yellowInputView 
    }
}

Running this gives what I would initially expect: a blue input view pops up.

enter image description here

But when I tap the button to switch to the yellow input view, nothing happens. Why? What do I need to do to get this to work?


Solution

  • After a little more experimenting I have the solution now. I needed to resign the first responder and then set it again. Any first responder that is a subview of the top view can be resigned indirectly by calling endEditing.

    @IBAction func changeInputViewButtonTapped(sender: UIButton) {
    
        let yellowInputView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
        yellowInputView.backgroundColor = UIColor.yellowColor()
    
        // first do this
        self.view.endEditing(true)
        // or this
        //textField.resignFirstResponder()
    
        textField.inputView = yellowInputView
        textField.becomeFirstResponder()
    }
    

    Thanks to this and this answer for the ideas.