How to change UIButton image in Swift

前端 未结 12 1342
猫巷女王i
猫巷女王i 2020-12-01 00:25

I am trying to change the image of a UIButton using Swift... What should I do

This is OBJ-C code.but I don\'t know with Swift:

[playButton setImage:[         


        
12条回答
  •  Happy的楠姐
    2020-12-01 01:12

    I prefer the method of initializing my variables at the top first:

    let playButton:UIButton!
    var image:UIImage!
    

    and then setting them in viewDidLoad

    override func viewDidLoad {
      ...
      playButton = UIButton(type: .Custom)
      imagePlay = UIImage(named:"play.png")
      playButton.setImage(imagePlay, forState: .Normal)
    }
    

提交回复
热议问题