How to open the keyboard automatically on UITextField?

我的未来我决定 提交于 2019-12-03 01:28:25

问题


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 touch the UITextfield.

Its all done in Interface Builder, so I am not sure how I do this. I guess I need to set the focus at some point ?

Thanks


回答1:


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.




回答2:


Swift 3 & 4:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    textField.becomeFirstResponder()
}



回答3:


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.



来源:https://stackoverflow.com/questions/2273470/how-to-open-the-keyboard-automatically-on-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!