I've been having some issues with calling -becomeFirstResponder
on a UITextField
contained with a view controller that is presented modally. I call this method in the modal view controller's -viewDidLoad
method so that the keyboard is immediately displayed. What I expected is for both the keyboard and the modal view controller to animate from up the bottom of the screen at the same time. However, what I'm observing is the following:
-presentModalViewController:animated:
method on the parent view controller and when the child view controller begins to animate modally.It's as if the keyboard's animation and the modal view controller's animation are both competing for some lower-level Core Animation resource at the same time, but I don't see why this should be happening. What further seems to corroborate this hunch is if I don't ask the UITextField
to become the first responder (i.e., if I don't ask the keyboard to present itself), then there is absolutely no UI lag, and the modal view controller animates instantly.
Interestingly, if I do something like [self.textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0001];
then the animation of the keyboard happens nearly at the same time as the modal view controller's animation -- it's extremely difficult to tell that they aren't both being animated at the exact same time when running on the iPhone Simulator. However, when running on an actual device, it's easily noticeable that the keyboard doesn't appear until after the modal view controller is presented. Importantly, though, there's no more UI lag.
Has anyone experienced anything similar to this?
I believe you're having problems because you're effectively stacking animations. The keyboard view is contained by the modal view. The keyboard view is trying to animate its slide in transition within the context of a view which is itself animating a slide in transition. The keyboard animation is trying to hit a moving target.
The pause is most likely the run time of the keyboard transition animation. I am fairly certain the the keyboard animation seizes priority from other animations so that it can drive the rearrangement of the UI e.g. scrolling a table so that the keyboard does not overlay the edited table row. In any case, the keyboard animation occurs within the context of the superview. This is especially true in the case of modal view.
So, the keyboard view animates itself sliding in but because the superview is not actually visible yet, you see nothing. When the superview does slide in, the keyboard is already present because its animation was completed before the superview started its animation.
In short, I don't think you can actual accomplish what you want to do. Instead, I think you will have to animate the modal view transition first, then run the keyboard animation or you will have to accept having the keyboard immediately visible.
I think that Cirrostratus' suggest above is a good one. Use an image of the keyboard that will slide in with the modal view and then immediately swap it out with the real keyboard.
The delayed keyboard animation bothered me as well. viewDidLayoutSubviews
was the magical method I was looking for. Placing the becomeFirstResponder
call there makes the keyboard slide up in time with the modal.
https://stackoverflow.com/a/19517739/3618864
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With