objective-c - 如何为所选的UIControlState设置按钮图像

标签 objective-c ios uibutton uicontrol

嗨,我想在按下按钮后更改按钮的背景,即按钮不应更改回前一张图像。

但是,我无法这样做。我的代码如下。任何人都可以建议我单击按钮后如何将图像更改为“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/

相关文章:

Objective-C:后台线程上的方法混合会发生太多递归?

ios - 使用 AVCaptureSession 在录制时发送视频数据

ios - 如何将UIButton的名称保存在字符串中?

ios - Swift 3 以编程方式创建的按钮不可点击

ios - 可以在上传前重命名 iOS 应用程序吗?

ios - 在 UIButton 中禁用字体动画

objective-c - NSViewController 相当于以下 UIViewController 函数/变量?

objective-c - 排序数组并在数组开头添加元素

objective-c - 核心数据 : Merging children added on a background context is funky

ios - Comicmind Material Framework 按钮事件处理程序