SLN SLN - 3 years ago 113
Swift Question

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

What I want:

A

textFiled
and a
textLabel
is included in the
tableView
. When the tableView is present the
textFiled
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
viewWillApper
or
viewDidLoad
(
textFiled
and
textLabel
is not showing at all).

How did I fixed it:

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


Question:

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

Many Thanks

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download