ios - UIButtonTypeSystem 为 UIControlStateHighlighted 设置背景图像

标签 ios objective-c uibutton ios7

在我所有的应用程序中,我都有带有 UIButtonTypeSystem 的 UIButton。 对于 ios7,我想以编程方式设置按钮外观。 对于正常状态,我需要带有蓝色边框颜色的白色背景 normal state button

对于突出显示的状态,我需要带有蓝色边框颜色的灰色背景 highlighted state button

(对于禁用状态,我需要带灰色边框的白色背景)

我为每个状态创建了图像(包含背景和边框颜色),并使用 UIButton 的方法 setBackgroungImage:forState: 设置它们。 但是当按钮处于高亮状态时,我得到了这个按钮样式 actual highlighted state button

我发现克服此行为的唯一方法是将所有应用程序按钮类型从 UIButtonTypeSystem 更改为 UIButtonTypeCustom。意味着传递所有应用程序 xib 的

有没有另一种不改变所有应用程序按钮类型的方法

谢谢 投资返回率

最佳答案

您好,您可以像这样以编程方式设置按钮:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[buuton setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(180/255.0) blue:(200/255.0) alpha:1]];
[view addSubview:button];

关于ios - UIButtonTypeSystem 为 UIControlStateHighlighted 设置背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19653531/

相关文章:

ios - 将CLLocationCoordinate2D存储在CoreData中可转换

iphone - 无法识别的选择器发送到实例...再次

iphone - 页面控制点没有改变

iphone - 运行项目问题(总是询问登录名和密码)

objective-c - 使用绘图 View 缩放

ios - 当用户按下按钮时显示 SplitViewController?

objective-c - lldb 无法打印 "error: reference to ' id' 的变量值不明确”

swift - UIButton 已调整大小以适合 UIImage

ios - 在 Swift 中任意按下更改自定义 UIButton 的 BG 颜色

ios - 为什么 UITableViewCell 上的 UIButton 仅在触摸手势持续短时间时才会变暗