Become first responder without animation

烂漫一生 提交于 2019-12-03 11:08:07
David Skrundz

You can use a UIView animation like so

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.0];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];

[textField becomeFirstResponder];  // <---- Only edit this line

[UIView commitAnimations];

This will cause the keyboard to suddenly appear.

You can do the same but with -resignFirstResponder

Swift ..

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.0)
    UIView.setAnimationDelay(0.0)
    someTextView.resignFirstResponder()
    UIView.commitAnimations()

Just set it as the first responder in -viewDidAppear to make the user never notice the field having lost its status.

-(void)viewWillAppear:(BOOL)animated {
    if ([self isViewLoaded] && self.textField)
        [self.textField becomeFirstResponder];
        [super viewWillAppear:animated];
}

I've put a short sample project up on dropbox using this code, if you'd like it.

I'm, mot sure what iOS version do you use or at what point do you call becomeFirstResponder Calling [textFieldReference becomeFirstResponder] in viewWillAppear: for iOS5/iOS6 seems to work just the way you wanted it to:

it's being called just before view controller will show it's view (that's if you don't handle view controller hierarchy manually) and as soon as it appears in navigation controller keyboard is already presented and text field has focus.

That said, there is no public way that I heard off that would allow to specify keyboard appearance style.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!