How to open the keyboard automatically on UITextField?

前端 未结 4 2042
借酒劲吻你
借酒劲吻你 2021-01-30 06:27

I have a very simple table and when tocuh a cell it opens a new view with one UITextfield. All I want is that the keyboard will automatically opens, without the user have to tou

相关标签:
4条回答
  • 2021-01-30 06:58

    Swift 3 & 4:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        textField.becomeFirstResponder()
    }
    
    0 讨论(0)
  • 2021-01-30 07:12

    To cause the keyboard to show up immediately you'll need to set the text field as the first responder using the following line:

    [textField becomeFirstResponder];
    

    You may want to place this in the viewDidAppear: method.

    0 讨论(0)
  • 2021-01-30 07:15
    override func viewDidLoad() {
        super.viewDidLoad()
        textField.becomeFirstResponder()
    }
    
    0 讨论(0)
  • 2021-01-30 07:17

    Prefer adding the first responder on the main thread -

     override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        DispatchQueue.main.async {
            self.textField.becomeFirstResponder()
        }
    }
    

    This will come in handy when view controller view is added as a subview.

    0 讨论(0)
提交回复
热议问题