AnthonyR AnthonyR - 2 months ago 16
Swift Question

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?

Answer

Try calling layoutIfNeeded on your view instead of textview.

UIView.animateWithDuration(0.5) {
    self.view.layoutIfNeeded()
}

Hope this helps.