Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...