Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to call becomeFirstResponder(), at ViewDidAppear or ViewWillAppear?

What I want:

A textField and a textLabel is included in the tableView. When the tableView is present the textField will be the firstResponder and brings out the keyboard.

Problems I have:

the App is only shows the keyboard if the textField.becomeFirstResponder() is called within viewWillAppear or viewDidLoad (textField and textLabel is not showing at all).

How I fixed it:

The bug is not reproducible when I moved the textField.becomeFirstResponder() call into viewDidAppear

Question:

Am I spouse to call becomeFirstResponder() function inside the viewDidAppear function? otherwise the keyboard will block all the other views

Many Thanks

like image 582
SLN Avatar asked Jan 04 '23 17:01

SLN


1 Answers

Think about what viewDidLoad, viewWillAppear, and viewDidAppear mean.

  • viewDidLoad means that the view controller has a view — but that view is not yet part of the interface.

  • viewWillAppear means that the view controller's view will become part of the interface — but hasn't yet.

  • viewDidAppear means that the view controller's view did become part of the interface.

You need your view to be part of the interface before your text field can summon the keyboard. Only then is the table view actually showing and configured. In other words, at this point the interface has settled down into its actual form, and we are ready to place the keyboard correctly above it.

like image 180
matt Avatar answered Jan 06 '23 05:01

matt