3

I am building a pin entry viewController very similar to the iOS one you see when you go to Settings->TouchID and it prompts you for pin.

I am trying to mimic its behavior of presenting the iOS keyboard along with (at the same) of the modal presentation of the pin entry viewController. I have noticed other applications like Venmo are able to achieve this as well.

How can I achieve this behavior? My pinEntryView is a textField. I have tried sending it the becomeFirstResponder message in viewDidAppear, and this seems to work; however, it will present the iOS keyboard AFTER the viewController modal presentation has finished. I want the presentation to occur at the same time to give the feeling that the iOS keyboard is actually baked-in/part of the ViewController.

I have tried sending the becomeFirstResponder messages in viewWillAppear, viewWillLayoutSubviews as well, but these are not stable solutions. Sometimes the keyboard is displayed and sometimes its not. Is there anyway to do this?

AyBayBay
  • 1,726
  • 4
  • 18
  • 37

2 Answers2

0

I made the test by setting becomeFirstResponder in the viewDidLoad and it is working fine. I have one button that calls the modal, and that modal has the next code:

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

It has no unexpected behavior.

  • Try testing it a couple times you will see the keyboard is sometimes presented and sometimes not. How did you test this? – AyBayBay Mar 06 '17 at 21:43
  • I hope this helps you. By the way, I am testing in physical device, not in simulator. [link](https://dl.dropboxusercontent.com/u/28337816/TestFirstResponder.zip) – Eduardo Antonio Munoz Mar 06 '17 at 21:53
  • You are able to consistently see the keyboard as presented? – AyBayBay Mar 06 '17 at 21:54
  • Yes, I am. I am able to see the same behavior than Venmo App when you select Pay or Request button and it displays a modal view that immediately opens the keyboard. – Eduardo Antonio Munoz Mar 06 '17 at 21:56
  • Seems this approach is no longer working in iOS16… No matter where I put the `becomeFirstResponder()` (I tried `viewDidLoad()`, `viewWillAppear()`, `viewDidAppear()`) the keyboard is showing after the modal is brought in, or doesn’t show at all… – Miko Lukasik Oct 12 '22 at 10:57
0

I believe I have had this issue before, and it has quite an interesting solution. What you want to do is call becomeFirstResponder in viewWillAppear before calling super .

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

Then when you call super the first responder is already set, and then iOS picks up this state and includes the keyboard appear animation in the appearance transaction.

Hopefully this can help you too.

George Green
  • 4,807
  • 5
  • 31
  • 45