如何为 UIControlStateSelected 设置按钮图像 [英] How to set the button image for UIControlStateSelected

查看:32
本文介绍了如何为 UIControlStateSelected 设置按钮图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按下按钮后更改按钮的背景,即按钮不应变回上一个图像.

Hi I want to change the background of my button after I press the button i.e. the button should not change back to the previous image.

但是,我无法这样做.我的代码如下.谁能建议我在单击按钮后如何将图像更改为custom_button_highlight"?

However, I am not able to do so. My code as follows. Can anyone advise how I can change the image to "custom_button_highlight" after I click on the button?

UIImage *buttonImageNormal = [UIImage imageNamed:@"custom_button"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal
                                         stretchableImageWithLeftCapWidth:12 topCapHeight:0];

UIImage *buttonImagePressed = [UIImage imageNamed:@"custom_button_highlight"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed
                                          stretchableImageWithLeftCapWidth:12 topCapHeight:0];

button.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[button setBackgroundImage:stretchableButtonImageNormal
                  forState:UIControlStateNormal];

[button setBackgroundImage:stretchableButtonImagePressed
                  forState:UIControlStateHighlighted];

[button setBackgroundImage:stretchableButtonImagePressed
                  forState:UIControlStateSelected];

推荐答案

您提到,您不希望图像变回...然后不是为突出显示或选择的状态设置它,而是必须为状态 UIControlStateNormal 设置它.

You mentioned, that you don't want the image to change back... then instead of setting it for the states highlighted or selected, you have to set it for the state UIControlStateNormal.

但是因为您在按下按钮后需要它,所以您必须捕捉按钮被按下的事件并在其中设置背景图像.

But since you need it after it's been pressed, you have to catch the event that your button has been pressed and set the background image in there.

希望有帮助;)

这篇关于如何为 UIControlStateSelected 设置按钮图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆