Search code examples
swiftkeyboardtvos

Open keyboard without clicking on TextField on tvOS - Swift


I'm creating an app and I need to login to it and I don't want to use 2 Text Field for insert Email and Password. What I would wish to achieve is: click on the button "Log in", then it will open the classic full screen keyboard that is usually appearing when you are doing click on UITextField in tvOS.

Can someone please write an example of code on how to invoke the keyboard clicking on a UIButton instead of UITextField? Thanks a lot!


Solution

  • While your UI is questionable, once your first text field finishes editing, you could start the other one.
    Set the first text field UITextFieldDelegate to the view controller, and then:

    class ViewController: UIViewController , UITextFieldDelegate{
    
        @IBOutlet weak var tf2: UITextField!
        @IBOutlet weak var tf1: UITextField!
    
        @IBAction func click() {
            tf1.becomeFirstResponder()
        }
    
        func textFieldDidEndEditing(_ textField: UITextField) {
            if (textField == tf1){
                tf2.becomeFirstResponder()
            }
        }
    
    }