Is it possible to animate UIEdgeInsets?
Asked Answered
P

1

6

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.

Playbook answered 9/5, 2015 at 6:50 Comment(3)
did u try to add more value to animate duration?... suppose 0.4Extrajudicial
Try calling self.searchField.layoutIfNeeded() inside the animation block. Does that help?Putamen
@robmayoff, you, my good sir, are brilliant. That did the trick. Want to post is as an answer so I can accept it?Playbook
P
5

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

Putamen answered 9/5, 2015 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.