UIButton:为选中状态设置图片

UIButton: set image for selected-highlighted state(UIButton:为选中状态设置图片)
本文介绍了UIButton:为选中状态设置图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为按钮的状态 Normal、Highlighted 和 Selected 设置了图像,但是当按钮处于选定状态并按下/突出显示它时,我没有看到突出显示的图像,而只是看到灰色的图像.选择按钮时是否可以设置图像为高亮状态?

I set an images for button's states Normal,Highlighted and Selected, but when the button in selected state and I press/highlight it I didn't see my highlighted image but just grayed picture. Is it possible to set an image for highlighted state when the button selected?

我的代码:

[button setImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];

当我这样做时:

[button setSelected:YES];

然后按下按钮,pressed.png"图像不会被选中.

and press the button, the "pressed.png" image doesn't select.

推荐答案

我找到了解决办法:需要添加添加行

I found the solution: need to add addition line

[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateSelected | UIControlStateHighlighted];

这篇关于UIButton:为选中状态设置图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
appearance().setBackgroundImage Not Working On Custom Class(外观().setBackoundImage在自定义类上不起作用)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
how can I check if my device is capable to render Emoji images correctly?(如何检查我的设备是否能够正确渲染Emoji图像?)
How to send an image with custom keyboard iOS(如何使用自定义键盘IO发送映像)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)