Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to animate UIEdgeInsets?

I've been trying to animate a change in UIEdgeInsets. I've already tried using UIView.animateWithDuration and the insets just jump to the final values. Does anyone know if this a property that can't be animated? Or better yet, is anyone aware of a way to animate insets?

As an example:

// Starting inset values
self.searchField.placeHolderInset = UIEdgeInsets(top: 0, left: 110, bottom: 0, right: 10)

// Animation block
UIView.animateWithDuration(0.1, animations: {
        self.searchField.placeHolderInset = UIEdgeInsets(top: 0, left: 18, bottom: 0, right: 10)
})

Instead of moving 92 pixels to the left over 0.1 seconds, the text jumps to the final value. searchField is a UITextField and .placeHolderInset is the UIEdgeInset being used to determine the insets for the UITextField's placeholder text.

EDIT: Another way to achieve the desired effect is by changing the UITextField's .textAlignment from .Center to .Left but that won't animate either.

like image 727
Clay Ellis Avatar asked May 09 '15 06:05

Clay Ellis


1 Answers

You need to call layoutIfNeeded() on the view, inside of the animation block.

like image 56
rob mayoff Avatar answered Oct 01 '22 10:10

rob mayoff