UIButton - image in default state, name in selected state

So, I am working on the second assignment for the winter of 2013 in the Stanford iOS / Objective-C class through iTunes U, and the very last part is driving me crazy.

This is a card game. Each map is represented as a UIButton. When the card object is face up, UIButton is in the selected state, and when the card is face down, UIButton is in the default state. After matching is found, the card may also be disabled.

The destination requests an image on the back of the card. In other words, each UIButton should show the image when it is in the default state, but then JUST the title property when it is in the selected or disabled state.

It seems simple enough ...

First I tried this in the graphical interface (Storyboard), setting a background image on my image, and then making sure that the selected and disabled states do not have an image. This led to the image being displayed, but then the title text simply showed over the image. Some searches have figured out the reason for this: unless it is explicitly stated that UIButton by default puts all other UIControlStates on UIControlStateNormal.

Now I'm trying to get this to work programmatically and still have no luck. All UIButtons that are cards are in the IBOutletCollection, which is an NSArray (cardButtons). In the viewDidLoad function, I have a for loop that goes through all the buttons:

UIImage *cardBack = [UIImage makeSomeImage...]; for (UIButton *oneButton in cardButtons) { [oneButton setBackgroundImage:cardBack forState:UIControlStateNormal]; [oneButton setBackgroundImage:nil forState:UIControlStateSelected]; } 

Which still gives me the same problem. It definitely successfully sets the image to its default state, but then when I click the button in the simulator, the image remains until the title just displays above it.

In the graphical interface and the software, I can get another image that will be displayed in the selected and disabled state. All I have to do is add:

 [oneButton setBackgroundImage:anotherImage forState:UIControlStateSelected]; 

In a loop, and all buttons show that another image has been selected.

What I can not do, and what drives me crazy, is trying to get there is not a background image when choosing so that UIButton shows only the name ...

Edit:

Although this is not an ideal solution (I think something might be possible using the imageView property in UIButton, but I'm not sure), I came up with a somewhat invented way to make this work.

First I created a UIImage, which I use for a class property called cardBackImage.

Then, in the updateUI function, which iterates over all the map buttons every time a model change is made, I added this line of code:

 [cardButton setBackgroundImage:(oneCard.isFaceUp ? nil : self.cardBackImage) forState:UIControlStateNormal]; 

So, every time a map object β€œflips”, UIButton must load or unload UIImage into or from its instance variable. Blech ... as I said, not perfect, but it works.

+4
source share
3 answers

I know this question was posted a long time ago, but I ran into the same problem and I found this answer to another post.

+1
source

Why aren't you trying to use an image instead of a backgroundImage button? It will hide the title text when the image is

 UIButton *button; [button setImage:[UIImage imageNamed:@"someImage"] forState:UIControlStateNormal]; 
0
source

I would suggest just changing the image when the button changes its state, that is, it touches. I would write a method like this:

 - (void)swapBackgroundImagesOfButton:(UIButton *)button { UIImage *normalImage = [button imageForState:UIControlStateNormal]; UIImage *selectedImage = [button imageForState:UIControlStateSelected]; [button setImage:selectedImage forState:UIControlStateNormal]; [button setImage:normalImage forState:UIControlStateSelected]; } 

Then, instead of adjusting the state of the selected button, you simply call this method when you show or hide the image.

0
source

All Articles