eemceebee
eemceebee

Reputation: 2666

How to open the keyboard automatically on UITextField?

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

Upvotes: 62

Views: 67843

Answers (4)

Yash
Yash

Reputation: 227

Prefer adding the first responder on the main thread -

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

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

Upvotes: 0

Shubham Goel
Shubham Goel

Reputation: 2176

override func viewDidLoad() {
    super.viewDidLoad()
    textField.becomeFirstResponder()
}

Upvotes: 1

NFC.cool
NFC.cool

Reputation: 3303

Swift 3 & 4:

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

Upvotes: 14

jessecurry
jessecurry

Reputation: 22116

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.

Upvotes: 139

Related Questions