【问题标题】:How to change custom UIButton's image on fly如何动态更改自定义 UIButton 图像
【发布时间】:2018-12-11 18:32:00
【问题描述】:

我创建了一个自定义 UIButton 类,使其看起来像下拉选择。我在按钮右侧有一个小的向下箭头图像。我想根据各种条件将按钮的图像更改为不同的图像,例如灰色、白色或红色。怎么做?以下是我的代码:

class DropDownButton: UIButton {

   let dropDownImageGrey = UIImage(named: "Icons/DropDown/Grey")
   let dropDownImageWhite = UIImage(named: "Icons/DropDown/White")
   let dropDownImageRed = UIImage(named: "Icons/DropDown/Red")

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        self.imageEdgeInsets = UIEdgeInsets(top: 0, left: self.frame.width - 108, bottom: 0, right:0)
        self.setImage(dropDownImageGrey, for: [])
        self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -13, bottom: 0, right:0)
    }
}

override func viewDidLoad() {
    // Change image to red one
    dropDownButton.??? // How to change?
}

【问题讨论】:

  • dropDownButton.setImage(dropDownButton.dropDownImageRed, for: .normal)?但是你最好使用静态常量。
  • Tx,你的意思是图像常量吗,这些 => let dropDownImageGrey = UIImage(named: "Icons/DropDown/Grey") ?

标签: ios swift uibutton


【解决方案1】:

您可能想使用UIButtonsetImage 属性,

override func viewDidLoad() {
    // Change image to red one
    let dropDownButton = DropDownButton()
    dropDownButton.setImage(dropDownButton.dropDownImageRed, for: .normal)
}

如果图片是在Assets.xcassets中添加的,那么你可以使用image literals或者直接使用类似的名字,

let dropDownImageGrey = UIImage(named: "Grey")
let dropDownImageWhite = UIImage(named: "White")
let dropDownImageRed = UIImage(named: "Red")

【讨论】:

    【解决方案2】:

    我认为上面的一些答案有效。但是,为了生产代码,我建议使用 enum 作为图像列表。

    class DropDownButton: UIButton {
    
        enum ImageType: String {
          case grey = "Icons/DropDown/Grey"
          case white = "Icons/DropDown/White"
          case red = "Icons/DropDown/Red"
    
          var image: UIImage? { return UIImage(named: rawValue) }
        }
    
        var imageType: ImageType = .red {
          didSet {
            setImage(imageType.image, for: .normal)
          }
        }
    
        override init(frame: CGRect) {
            super.init(frame: frame)
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    
        override func awakeFromNib() {
            super.awakeFromNib()
    
            self.imageEdgeInsets = UIEdgeInsets(top: 0, left: self.frame.width - 108, bottom: 0, right:0)
            self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -13, bottom: 0, right:0)
        }
    }
    
    override func viewDidLoad() {
        // Change image to red one
        dropDownButton.imageType = .red
    }
    

    以后如果需要更改图片类型,只需设置按钮的imageType即可。喜欢

    dropDownButton.imageType = .grey
    dropDownButton.imageType = .white
    

    【讨论】:

    • 有趣的想法。
    【解决方案3】:

    您可以使用枚举来定义各种条件并添加获取图像的方法。根据条件,您可以设置图像。示例如下:

    在你的类之外定义这个枚举

    enum DropdownCondition
    {
        case condition1
    
        case condition2
    
        case condition3
    
        func getImage() -> UIImage? {
            switch self {
            case .condition1:
                return UIImage(named: "Icons/DropDown/Grey")
            case .condition1:
                return UIImage(named: "Icons/DropDown/White")
            case .condition1:
                return UIImage(named: "Icons/DropDown/Red")
            default:
                return nil
            }
        }
    }
    

    在您的 viewDidLoad/init 或任何基于条件调用 yourMethodWithSomeoCndition(.condition1) 的方法中。

    override func viewDidLoad() {
        // Change image to red one
        let dropDownButton = DropDownButton()
    
        //Call based on your condition
        yourMethodWithSomeoCndition(.condition1)//This condition can change on the fly
    }
    
    
    
    func yourMethodWithSomeoCndition(_ condition:DropdownCondition)
    {
        self.dropDownButton.setImage(condition.getImage(), for: .normal)
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-15
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      • 2014-01-23
      • 2011-11-24
      相关资源
      最近更新 更多