我使用下面显示的代码创建了一个按钮 -
UIImage *kalenderImage = [UIImage imageNamed:@"start_icon_calendar_u.png"];
UIImageView *kalenderImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kalenderImage.size.width/2, kalenderImage.size.height/2)] autorelease];
[kalenderImageView setImage:kalenderImage];
UILabel* kalendarLabel = [[[UILabel alloc] initWithFrame:CGRectMake (0, kalenderImage.size.height/2-15, kalenderImage.size.width/2, kalenderImage.size.height/2)] autorelease];
kalendarLabel.text = @"Kalender";
[kalenderButton addSubview:kalenderImageView];
[kalenderButton addSubview:kalendarLabel];
[kalenderButton addTarget:self action:@selector(showCalendar:) forControlEvents:UIControlEventTouchUpInside];
我需要更改UIControlStateHighlighted
状态按钮的图像。我怎样才能做到这一点 ?
我不想用
KalenderButton setBackgroundImage:@"" forState:]
[KalenderButton setImage:@"" forState]
答案 0 :(得分:0)
非常简单。请查看以下代码。它检查文件是否已锁定并相应地更改按钮的图像。您可以根据需要编辑代码。
if (fileLocked) {
UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:@"icon-lock.png"]];
[lockButton setImage:image forState:UIControlStateNormal];
} else {
UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:@"icon-unlock.png"]];
[lockButton setImage:image forState:UIControlStateNormal];
}
有效。