Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing keyboard at the right time iOS7

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).

like image 935
Giuseppe Garassino Avatar asked Sep 26 '13 09:09

Giuseppe Garassino


1 Answers

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.

like image 115
Ell Neal Avatar answered Sep 27 '22 16:09

Ell Neal