How to change UIButton image in Swift

前端 未结 12 1298
猫巷女王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条回答
  • 2020-12-01 01:06

    Yes, even we can change image of UIButton, by using flag.

    class ViewController: UIViewController
    {
        @IBOutlet var btnImage: UIButton!
        var flag = false
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
    
            //setting default image for button
            setButtonImage()
    
        }
    
        @IBAction func btnClick(_ sender: Any)
        {
            flag = !flag
            setButtonImage()
        }
    
        func setButtonImage(){
            let imgName = flag ? "share" : "image"
            let image1 = UIImage(named: "\(imgName).png")!
            self.btnImage.setImage(image1, for: .normal)
        }
    
    }
    

    Here, after every click your button image will change alternatively.

    0 讨论(0)
  • 2020-12-01 01:10

    you can actually do this by highlighting the button and within the insepctor on the right hand tool bar you can update the image. obviously you can do it in code also as stated previously but this is another option for you

    0 讨论(0)
  • 2020-12-01 01:11

    From your Obc-C code I think you want to set an Image for button so try this way:

    let playButton  = UIButton(type: .Custom)
    if let image = UIImage(named: "play.png") {
        playButton.setImage(image, forState: .Normal)
    }
    

    In Short:

    playButton.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)
    

    For Swift 3:

    let playButton  = UIButton(type: .custom)
    playButton.setImage(UIImage(named: "play.png"), for: .normal)
    
    0 讨论(0)
  • 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)
    }
    
    0 讨论(0)
  • 2020-12-01 01:12

    In Swift 4.2 and Xcode 10.1

    Add image for selected UIButton

    button.addTarget(self, action: #selector(self.onclickDateCheckMark), for: .touchUpInside)//Target 
    button.setImage(UIImage.init(named: "uncheck"), for: UIControl.State.normal)//When selected
    button.setImage(UIImage.init(named: "check"), for: UIControl.State.highlighted)//When highlighted
    button.setImage(UIImage.init(named: "check"), for: UIControl.State.selected)//When selected
    

    But if you want to change selected button image you need to change it's selected state. Then only selected image will appear in your button.

    @objc func onclickDateCheckMark(sender:UIButton) {
        if sender.isSelected == true {
            sender.isSelected = false
            print("not Selected")
        }else {
            sender.isSelected = true
            print("Selected")
    
        }
    }
    
    0 讨论(0)
  • 2020-12-01 01:17

    Swift 5

    yourButton.setImage(UIImage(named: "BUTTON_FILENAME.png"), for: .normal)
    
    0 讨论(0)
提交回复
热议问题