Become first responder without animation

前端 未结 3 673
無奈伤痛
無奈伤痛 2021-02-13 18:31

Is there a way for a UITextField to become first responder without the animation of the keyboard? That is, make it such that the keyboard just appears?

Basi

相关标签:
3条回答
  • 2021-02-13 18:45

    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.

    0 讨论(0)
  • 2021-02-13 18:48

    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.

    0 讨论(0)
  • 2021-02-13 19:03

    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()
    
    0 讨论(0)
提交回复
热议问题