11

In iOS 6 I'm used to present keyboard in viewDidLoad.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [txtField becomeFirstResponder];
}

This way, when navigationController pushes the new viewController, keyboard is already there, animating smoothly from left to right and avoiding bottom-up animation.

In iOS 7 this behavior seems broken.

If I add [txtField becomeFirstResponder] in viewDidLoad, keyboard appears in the middle of pushing animation, already in its final position: an unpleasant effect!!

I've tried to move [txtField becomeFirstResponder] in viewWillAppear, but the final result is unchanged.

Do you know a way to get back iOS 6 behavior, pushing the new viewController and the keyboard all together?

EDIT: Using a timer doesn't work either... whatever time delay I set, the keyboard is shown only at the end of pushing animation.

So far, my best try it is to put [txtField becomeFirstResponder] in viewWillLayoutSubviews or viewDidLayoutSubviews. Unfortunately, doing so working when pushing viewController but not when popping back (the keyboard doesn't appear).

Giuseppe Garassino
  • 2,272
  • 1
  • 27
  • 47

1 Answers1

13

I've managed to extrapolate your workaround in viewWillLayoutSubviews to force it to work.

- (void)viewWillLayoutSubviews {

    if (![self.textField1 isFirstResponder] && ![self.textField2 isFirstResponder] && ...) {
        [self.textField1 becomeFirstResponder];
    }
}

This is working for me for both pushing onto the stack, and after dismissing a modal view controller.

Ell Neal
  • 6,014
  • 2
  • 29
  • 54
  • 3
    Awesome!.. this was exactly what I needed.. ViewWillAppear is too soon and ViewDidAppear is too late. – delux247 Jan 23 '14 at 23:31
  • `viewWillLayoutSubviews` was the right method for me to use, but I also needed to wrap `becomeFirstResponder` method in to `perform:selector` as follow: `textView.perform(#selector(becomeFirstResponder), with: nil, afterDelay: 0)` – salabaha Feb 01 '22 at 20:18