嗨,我想在按下按钮后更改按钮的背景,即按钮不应更改回前一张图像。
但是,我无法这样做。我的代码如下。任何人都可以建议我单击按钮后如何将图像更改为“custom_button_highlight”吗?
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状态,而不是将其设置为突出显示或选定的状态。
但是由于在按下按钮后需要它,因此必须捕获按钮被按下的事件并在其中设置背景图像。
希望它有所帮助;)
关于objective-c - 如何为所选的UIControlState设置按钮图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7136510/