Modern UIButton
configuration API allows for new way of setting button appearance. However the code below skips the animation entirely.
@IBAction func buttonTouched(_ sender: UIButton) {
sender.configuration?.background.backgroundColor = .green
UIView.animate(withDuration: 0.4, delay: 0.5) {
sender.configuration?.background.backgroundColor = .systemMint
}
}
Similar thing can be done like this:
@IBAction func buttonTouched(_ sender: UIButton) {
sender.backgroundColor = .red
UIView.animate(withDuration: 0.4, delay: 0.5) {
sender.backgroundColor = .systemMint
}
}
And this works. The question is how to animate UIButton
‘s configuration changes.
2
Answers
The way to achieve animated configuration change by using transitions is the only workaround I could find so far. Somewhat like this:
When I tried to optimize and reuse code, I ended with something like:
After some quick searching, it appears
configuration.background.backgroundColor
is not animatable.Depending on your needs, you can use a
.customView
for the button’s background and then animate the color change for that view.Quick example (assuming you’ve added the button as an
@IBOutlet
):