UIButton图像未显示正常状态

时间:2016-08-31 08:25:07

标签: ios xcode uibutton uikit xcode7

我正在尝试使用按钮复选框,这就是我正在做的事情:

=:=

但只有当我触摸按钮时图像才会出现,在任何其他状态下图像都不会显示。有什么遗失的吗?

1 个答案:

答案 0 :(得分:1)

您必须更改要更改的图像的按钮状态。使用以下代码来获得所需的结果。

int checkBoxWidth = foregroundHeight / 12.0;
int checkBoxHeight = foregroundHeight / 12.0;
UIButton* checkBox = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, checkBoxWidth, checkBoxHeight)];
[checkBox setImage:[Utility getEmptyCheckBoxOutIcon] forState:UIControlStateNormal];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateHighlighted];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected | UIControlStateHighlighted];
[checkBox addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
checkBox.layer.borderWidth = 1.0;
checkBox.layer.borderColor = [Utility primaryColor].CGColor;
checkBox.layer.cornerRadius = cornerRadius;
[switchView addSubview:checkBox];

并添加此目标函数

-(IBAction)buttonClicked:(UIButton*)sender{

sender.selected = !sender.selected;

}