簡體   English   中英

如何動態更改自定義UIButton的圖像

[英]How to change custom UIButton's image on fly

我創建了一個自定義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?
}

您可能要使用UIButton setImage屬性,

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")

我認為以上工作有一些答案。 但是,出於生產代碼的考慮,我建議對圖像列表使用枚舉。

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

您可以使用枚舉定義各種條件並添加獲取圖像的方法。 根據條件可以設置圖像。 示例如下:

在課程之外定義該枚舉

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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM