简体   繁体   English

UIButton - 默认状态下的图像,选定状态下的标题

[英]UIButton - Image in Default State, Title in Selected State

So I'm working my way through the second assignment for the Winter 2013 Stanford iOS/Objective-C class through iTunes U, and the very last part is driving me crazy.所以我正在通过 iTunes U 完成 2013 年冬季斯坦福 iOS/Objective-C 课程的第二个作业,最后一部分让我发疯。

This is a card matching game.这是一款卡牌配对游戏。 Each card is represented in the view as a UIButton.每张卡片在视图中都表示为一个 UIButton。 When the card object is face up the UIButton is in selected state, and when the card is face down the UIButton is in default state.当卡片对象面朝上时 UIButton 处于选中状态,当卡片面朝下时 UIButton 处于默认状态。 A card can also be disabled after a match is found.找到匹配项后,也可以禁用卡片。

The assignment asks to put an image on the back of the cards.作业要求将图像放在卡片背面。 In other words, each UIButton needs to show an image when it is in the default state, but then JUST the title property when it is in the selected or disabled state.换句话说,每个 UIButton 需要在默认状态下显示一个图像,但是当它处于选中或禁用状态时只显示 title 属性。

Seems simple enough...看起来很简单...

First I tried this in the GUI (Storyboard), setting the background image to my image, and then making sure both the selected and disabled states had no image.首先,我在 GUI(故事板)中尝试了这个,将背景图像设置为我的图像,然后确保选中和禁用状态都没有图像。 This resulted in the image showing, but then the title text just showing over the image.这导致图像显示,但标题文本只是显示在图像上。 Some searching turned up the reason for this: unless explicitly told so UIButton defaults all other UIControlStates to UIControlStateNormal.一些搜索发现了这个原因:除非明确告知 UIButton 将所有其他 UIControlStates 默认为 UIControlStateNormal。

Now I've been trying to get this to work programmatically and still no luck.现在我一直试图让它以编程方式工作,但仍然没有运气。 All the UIButtons which are cards are in an IBOutletCollection which is an NSArray (cardButtons).所有作为卡片的 UIButtons 都在一个 IBOutletCollection 中,它是一个 NSArray(cardButtons)。 In the viewDidLoad function I have a for loop that goes through all the buttons:在 viewDidLoad 函数中,我有一个遍历所有按钮的 for 循环:

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 is definitely successfully setting the image for the default state, but then when I click on the button in the simulator, the image remains while the title simply displays over it.它绝对成功地将图像设置为默认状态,但是当我单击模拟器中的按钮时,图像仍然存在,而标题只是显示在它上面。

In both the GUI and programmatically I can get a different image to show up in selected and disabled state fine.在 GUI 和编程中,我都可以得到一个不同的图像,以显示在选定和禁用状态下。 All I have to do is add:我所要做的就是添加:

[oneButton setBackgroundImage:anotherImage forState:UIControlStateSelected];

To the loop and all the buttons show that other image when selected.循环和所有按钮在选择时显示其他图像。

What I cannot do, and what is driving me crazy, is trying to get there to be no background image when selected so that the UIButton only shows the title...我不能做的,以及让我发疯的事情,是试图在选择时没有背景图像,以便 UIButton 只显示标题......

Edit:编辑:

While not an ideal solution (I think something might be possible using the imageView property in UIButton, but I'm not sure), I have come up with a somewhat tacked-on feeling way to make this work.虽然不是一个理想的解决方案(我认为使用 UIButton 中的 imageView 属性可能会有一些事情,但我不确定),但我想出了一种有点固定的感觉方法来完成这项工作。

First I made the UIImage I'm using into a class property called cardBackImage.首先,我将我正在使用的 UIImage 制作成一个名为 cardBackImage 的类属性。

Then, in the updateUI function that loops through all the card buttons each time a change is made to the model, I added this line of code:然后,在每次对模型进行更改时循环遍历所有卡片按钮的 updateUI 函数中,我添加了以下代码行:

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

So every time a card object is "flipped" that UIButton has to load or unload a UIImage into or out of its instance variable.因此,每次卡片对象“翻转”时,UIButton 都必须将 UIImage 加载或卸载到其实例变量中或从中卸载。 Bleh... like I said, not ideal, but it works. Bleh...就像我说的,不理想,但它的工作原理。

why dont you try the image Property instead of backgroundImage of the button ?It will hide the title text when an image is there你为什么不试试按钮的图像属性而不是背景图像?当有图像时它会隐藏标题文本

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

I would suggest to simple change the image when the button changes it's state ie get's touched.我建议在按钮改变它的状态时简单地改变图像,即被触摸。 I'd 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 setting the selected state of the button, you simply call that method, when you the image to show or hide.然后,当您要显示或隐藏图像时,您只需调用该方法,而不是设置按钮的selected状态。

Here is a simpler solution that let you use just the storyboard to set the UIButton image and title, according to the state.这是一个更简单的解决方案,可让您根据状态仅使用情节提要来设置 UIButton 图像和标题。 Inside the attribute inspector you set the button image for default state, then you set the button title for the selected state and digit a whitespace character for the image as you can see在属性检查器中,您将按钮图像设置为默认状态,然后为所选状态设置按钮标题,并为图像设置一个空白字符,如您所见

在此处输入图片说明

then you can change the state programmatically by self.button.isSelected = true and see the image turn into text然后您可以通过self.button.isSelected = true编程方式更改状态并查看图像变成文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM