Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView - Animate with duration seems to ignore duration

I'm facing a problem : I have a very simple custom UIView which groups one UITextField and one UILabel.

The UILabel is on the UITextField, and I want that the UILabel go up to 40px when user begins to edit the UITextField.

I'm using constraints to set the textfield's frame and the label's frame.

So I just set the delegate in my custom view with UITextFieldDelegate protocol, and implement the delegate method : textFieldDidBeginEditing, as bellow :

I'm just animating the constraint (center Y) of my label in the delegate method.

func textFieldDidBeginEditing(textField: UITextField) {
    self.centerVerticallyLabelConstraint.constant = -40
    UIView.animateWithDuration(0.5) {
        self.textFieldLabel.layoutIfNeeded()
    }
}

What happens is that the UILabel goes up correctly, but it goes up instantly. It ignores the duration of 0.5 in my animation. I don't know why :

Show problem image

I'm not animating something else at the same time, I only have this animation. I haven't disable animations for UIView. The delegate is set to my UITextfield.

Any idea?

like image 332
AnthonyR Avatar asked Sep 13 '16 11:09

AnthonyR


1 Answers

Try calling layoutIfNeeded on your view instead of textview.

UIView.animate(withDuration: 0.5) {
    self.view.layoutIfNeeded()
}

Hope this helps.

like image 126
nishith Singh Avatar answered Sep 28 '22 04:09

nishith Singh