admin管理员组

文章数量:1123790

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!

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!

Share Improve this question edited yesterday Aviram Netanel asked yesterday Aviram NetanelAviram Netanel 13.6k9 gold badges49 silver badges72 bronze badges 2
  • A storyboard button is configuration based. You need to change its configuration's background's image. – matt Commented yesterday
  • @matt sorry, I forgot to mention "extension UIButton" at the beginning... hope now it makes more sense. you are right, but I'm trying to change the image programmatically. – Aviram Netanel Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: UIButton setBackgroundImage doesn39t work Swift UIKitStack Overflow