How to change button image in swift?

后端 未结 8 845
陌清茗
陌清茗 2021-02-10 04:09

I am working on an app which has a button. The button has no text, image or background.

So what I want to do is to give it an image in the viewDidLoad function.

8条回答
  •  醉酒成梦
    2021-02-10 04:25

    You don't need ".png".

    If ".imageWithRenderingMode(.AlwaysOriginal)" is working for you: To keep the same image in different states, you have to set the same image/properties for the different states.

    @IBOutlet var tapButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        tapButton.setImage(UIImage(named: "redTap")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
        tapButton.setImage(UIImage(named: "redTap")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Highlighted)
    }
    

提交回复
热议问题