Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display keyboard without animation

Looked intoUIKeyboardAnimationDurationUserInfoKey but I just can't find anywhere how to set it to a custom value.

like image 945
samvermette Avatar asked Dec 05 '09 08:12

samvermette


4 Answers

UIKeyboardAnimationDurationUserInfoKey is a const string identifier for the dictionary key that holds the animation duration, so there is no way to change it easily.

One way to make the keyboard appear without animation is to observe the keyboard notifications and disable animation when it's about to appear and then reenable them. This, of course, disables any other animation as well.

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(willShowKeyboard:) 
                                             name:UIKeyboardWillShowNotification 
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(didShowKeyboard:) 
                                             name:UIKeyboardDidShowNotification 
                                           object:nil];

- (void)willShowKeyboard:(NSNotification *)notification {
    [UIView setAnimationsEnabled:NO];
}

- (void)didShowKeyboard:(NSNotification *)notification {
    [UIView setAnimationsEnabled:YES];
}

and then then the same for UIKeyboardWillHideNotification/UIKeyboardDidHideNotification notifications.

like image 126
monowerker Avatar answered Oct 31 '22 20:10

monowerker


Try

[UIView performWithoutAnimation:^{
    [textField becomeFirstResponder];
}];
like image 34
goetz Avatar answered Oct 31 '22 20:10

goetz


iOS8-compatible:

Add the appropriate delegate method:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [UIView setAnimationsEnabled:NO];
}

or

- (void)textViewDidBeginEditing:(UITextView *)textView {
    [UIView setAnimationsEnabled:NO];
}

Add the keyboard notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];

And method:

- (void)didShowKeyboard:(NSNotification *)notification {
    [UIView setAnimationsEnabled:YES];
}
like image 9
Vadoff Avatar answered Oct 31 '22 20:10

Vadoff


I've found the best solution is using UIView.setAnimationsEnabled(_ enabled: Bool).

Swift 3

UIView.setAnimationsEnabled(false)
textField.becomeFirstResponder()
// or textField.resignFirstResponder() if you want to dismiss the keyboard
UIView.setAnimationsEnabled(true)
like image 8
Saoud Rizwan Avatar answered Oct 31 '22 20:10

Saoud Rizwan