samvermette
samvermette

Reputation: 40437

Display keyboard without animation

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

Upvotes: 27

Views: 22413

Answers (7)

monowerker
monowerker

Reputation: 2979

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.

Upvotes: 58

Saoud Rizwan
Saoud Rizwan

Reputation: 659

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)

Upvotes: 9

goetz
goetz

Reputation: 916

Try

[UIView performWithoutAnimation:^{
    [textField becomeFirstResponder];
}];

Upvotes: 10

Baran
Baran

Reputation: 2810

The answer of @Vadoff works perfect. Here for Swift 3:

    override func viewDidLoad() {
        super.viewDidLoad()

        //...

        // Add observer to notificationCenter so that the method didShowKeyboard(_:) is called when the keyboard did show.
        NotificationCenter.default.addObserver(self, selector: #selector(type(of: self).didShowKeyboard(_:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)

        // Make textField the first responder.
        textField.becomeFirstResponder() // <- Change textField to the name of your textField.
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        // Disable animations.
        UIView.setAnimationsEnabled(false)
    }
    func didShowKeyboard(_ notification: Notification) {
        // Enable animations.
        UIView.setAnimationsEnabled(true)
    }

Upvotes: 4

Anthony Tran
Anthony Tran

Reputation: 40

It is pretty simple guys. Do not use UIView:SetAnimationEnabled as that will be potentially troublesome. This is how i remove animation when keyboard show.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [txtFirstName becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [txtFirstName resignFirstResponder];
}

Upvotes: -8

Nevena Aleksieva
Nevena Aleksieva

Reputation: 1

I had to disable Animations in textViewShouldBeginEditing, textFieldDidBeginEditing did not work for me (iOS 8)

Upvotes: -1

Vadoff
Vadoff

Reputation: 9419

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];
}

Upvotes: 9

Related Questions