1

I have a UIViewController with a UIScrollView inside of it. This scroll view contains some UITextFields inside of it and I want to be able to scroll the view according to the field in focus. My question is: is there a way to detect when the first responder in a UIViewController change? So I can get it's frame and change the scroll view offset to make it on top of the keyboard. I already use the keyboardWillShowNotification to get keyboard size changes. But when keyboard is already being shown and another text field becomes the first responder no changes are made in the keyboard and I can't detect which is the new first responder.

Updates:

  • The Scroll View is in a Container View Controller, so the text fields are added and UITextFieldDelegate may not work for this. Unless I can set the delegates after adding the content of the container.
  • I already manage the keyboard notifications, they don't cover all cases.
  • There are UITextViews in the form too.
3
  • 1
    Text field notifications Commented Feb 8, 2019 at 0:08
  • developer.apple.com/library/archive/documentation/… Commented Feb 8, 2019 at 2:40
  • I already manage keyboard notifications. But they don't cove the case where I keep the keyboard unchanged but change the first responder. Commented Feb 8, 2019 at 6:58

1 Answer 1

1

You should make your UIViewController the delegate of the UITextfields and in your implementation of func textFieldDidBeginEditing(UITextField), you can find out which text field is being edited.

For reference: https://developer.apple.com/documentation/uikit/uitextfielddelegate

Sign up to request clarification or add additional context in comments.

3 Comments

The Scroll View is in a Container View Controller, so the text fields are added later in a container view, so UITextFieldDelegate may not work for this. Or is there a way to set these delegates later?
You can also set the delegates in viewDidAppear if you want to set them when everything is already loaded in right?
@RicardoGehrkeFilho Yes, you can set delegates at any time you want. All you need is a reference to the text field. Then you can just say textfield.delegate = self (or whatever you want the delegate to be) I'm not sure I understand the particulars of what you're saying about your view hierarchy. Are you using storyboards or adding the views in code?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.