1

I have named a set of images like this:

Unselected:

nameOfTheImage

Selected:

nameOfTheImage~

In order to change the background image of a button, my code is written to take the string of the background image's name, and either append or take off the "~" to change it to the correct image.

So I tried this:

let backgroundString = String(selector.currentBackgroundImage!)
print(backgroundString)

But I get this:

<UIImage: 0x146d98b80>, {485, 300}

Is there any way to get the plain name of the background image?

Any help is greatly appreciated.

7
  • 1
    I think this isn't possible to get the name of the background image of a uibutton. refer these stackoverflow.com/questions/3158737/… . as a suggestion, how about to use uiimageview instead of a button background image Commented Aug 12, 2016 at 16:34
  • @AnuradhS Do you get image name from UIImageView? Commented Aug 12, 2016 at 16:44
  • nope, but it is easy to change images by checking which one is displaying than checking button background image Commented Aug 12, 2016 at 16:53
  • can you show the code of button Commented Aug 12, 2016 at 17:14
  • 2
    What do you intend to do? You can set background image differentely by state of UIButton i.e.. UIControlStateSelected.. Commented Aug 12, 2016 at 17:17

3 Answers 3

2

i had a similar problem. Fortunately I know the state of the button when the viewcontroller is displayed, so I created a String control, which was something like this:

var buttonState = "not selected" 

then when the user clicks on the button I check:

if (buttonState == "not selected") {

   // change buttonState to "selected"

   // change the image of the button

}
Sign up to request clarification or add additional context in comments.

1 Comment

Why use a string instead of a bool?
1

I just dealt with a somewhat similar problem. You can use: buttonName.setBackgroundImage(#imageLiteral(resourceName: "backgroundImageName"), for: .normal) Where here .normal is the state of the button, so you can adjust your state accordingly.

For anyone else who might be having this problem, this is for Swift5.

Comments

1

I was able to retrieve the button's background image name by doing the following:

let imgDesc : String = button.currentBackgroundImage!.description

The above string (imgDesc) always contains something similar to the following:

<UIImage:0x281646400 named(main: userBike) {40, 63}>

Using prefix I drop everything after the close parenthesis in a new string variable:

var imgName = imgDesc.prefix { $0 != ")" } //always contains only one close parenthesis

Using range I retrieve the index of where "(main: " is located within my imgName variable:

let index = imgName.range(of: "(main: ")?.upperBound //always contains open parenthesis followed by main:<space>

Using the index from above I drop everything before it:

imgName = imgName.suffix(from: index!)

This ultimately isolates the image name from button.currentBackgroundImage.description:

print(imgName) //prints userBike

Prints "userBike" which is the image name successfully isolated from imgDesc.

3 Comments

Sorry. Not sure what I did wrong. Is it not an answer?
No, everything seems good.
All I meant, was that I tried to think of a way to prevent it from breaking in the future if the output of currentBackgroundImage.description ever changed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.