Aviram Netanel
Aviram Netanel

Reputation: 13675

UIButton setBackgroundImage doesn't work?! Swift UIKit

I have a UIButton in my UIViewController in my storyboard, and I'm trying to change it's background image, using transition animation:

  extension UIButton {
func setBackgroundImageWithFade(_ image: UIImage?, 
duration: TimeInterval = 1.25, 
completion : ((Bool) -> Void)? = nil) {
  // Create a crossfade animation
  UIView.transition(with: self, duration: duration, options: .transitionCrossDissolve, animations: {
      self.setBackgroundImage(image, for: .normal)
  }, completion: completion)
}
}

I've tried to change the button's type, and also the Background property is set to 'Custom'. but nothing works - the image won't change!

enter image description here

Upvotes: -1

Views: 51

Answers (1)

Aviram Netanel
Aviram Netanel

Reputation: 13675

So this is the solution that worked for me:

on the button's attributes inspector panel I changed the Type -> Custom and also the Style -> Default.

enter image description here

Upvotes: 0

Related Questions