2

I want to change the image of a UIButton for different states. To achieve this, I'm using:

btn.setImage(UIImage(named: "blabla"), for .normal)

and

btn.setImage(UIImage(named: blabla2), for .disabled)

This only makes some appear dimmed.

What did I do wrong? I just want to make my button appearance the same for different states, how?

(my button type - .system).

NSNoob
  • 5,548
  • 6
  • 41
  • 54
Dmitriy Greh
  • 684
  • 8
  • 17
  • 1
    You cant change system type button image, only custom button can change image – Tj3n Dec 07 '16 at 08:48
  • 1
    Possible duplicate of [How to change button image in swift?](http://stackoverflow.com/questions/38041688/how-to-change-button-image-in-swift) – Sasha Kozachuk Dec 07 '16 at 08:49
  • 3
    Possible duplicate of [How to change UIButton image in Swift](http://stackoverflow.com/questions/26837371/how-to-change-uibutton-image-in-swift) – dirtydanee Dec 07 '16 at 08:55
  • Thank you guys. I found a solution. How to close the question? It's my first question) –  Dmitriy Greh Dec 07 '16 at 08:57
  • You can close or delete it, by selecting one of the options under the question (share edit close delete flag). – Ahmad F Dec 07 '16 at 08:58

6 Answers6

2

This helped me (swift 3.0)

btn.setImage(UIImage(named:"yourFriend")?.withRenderingMode(.alwaysOriginal), for: .normal)

btn.setImage(UIImage(named:"yourFriend")?.withRenderingMode(.alwaysOriginal), for: .disabled)
JPetric
  • 3,838
  • 28
  • 26
Dmitriy Greh
  • 684
  • 8
  • 17
1

You just need to set one for the state. And if you don't set another image for different state. It would look the same in all state.

button.setImage(image, forState: .Normal)

How to change UIButton image in Swift

Community
  • 1
  • 1
Taj Ahmed
  • 895
  • 11
  • 19
1

For display disabled button set image

let btn  = UIButton(type: .Custom)
btn.setImage(UIImage(named: blabla2), for .disabled)

Then

btn.enabled = false // to display Disable image
btn.enabled = true // to display Normal image
jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29
1
    private let button1: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named:"firstButtonNormalStateImage"), for: .normal)
        button.setImagesetImage(UIImage(named:"firstButtonSelectedStateImage"), for: .selected)
        return button
    }()

    private let button2: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named:"secondButtonNormalStateImage"), for: .normal)
        button.setImage(UIImage(named:"secondButtonSelectedStateImage"), for: .selected)
        return button
    }()

    // implement for example in viewDidLoad()

    button1.addTarget(self, action: #selector(firstButtonDidTap), for: .touchUpInside)
    button2.addTarget(self, action: #selector(secondButtonDidTap), for: .touchUpInside)

// trigger actions

    @objc func firstButtonDidTap() {
        button1.isSelected = true
        button2.isSelected = false
    }

    @objc func secondButtonDidTap() {
        button2.isSelected = true
        button1.isSelected = false
    }
janaz
  • 673
  • 5
  • 12
1

For whoever is still having this issue (currently Xcode 10.0) with a Custom button, I found I was able to change the text and/or image if instead of:

myButton.setTitle("Hi", for: [.normal])

I used this:

myButton.setTitle("Hi", for: []) //remove specific states

I don't know why .normal was not working for me, even though the button was definitely enabled. But maybe this will save someone else a headache!

Merricat
  • 2,583
  • 1
  • 19
  • 27
0

You can simply do this by StoryBoard as well. Select the button, got to identity inspector and do the following:- Firstly set the buttonType to custom instead of system. Secondly choose state Config to lets say default and give the imageName in "image" attribute, similarly choose other state configs (Highlighted, disabled, selected etc.) and set images as required by you. Then later in the code you just have to control and set the state of the button, and respective image will be shown to you.

Ishika
  • 2,187
  • 1
  • 17
  • 30