Vote count:
0
So I'm creating a UIButton and I want to set it to have two separate images for different states (normal and highlighted) but when setting the highlighted property to yes, the highlighted image gets drawn on top of the normal state image, instead of replacing it completely.
Here's my code:
CGFloat size = 50.0;
CGRect frame = CGRectMake(0, 0, size, size);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundColor:[UIColor clearColor]];
[button setFrame:frame];
[button setImage:[UIImage imageNamed:@"image1"]forButtonState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"image2.jpg"] forButtonState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:@"image2.jpg"] forButtonState:UIControlStateHighlighted];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
-(void)buttonPressed:(UIButton *)sender
{
[sender setSelected:YES];
[sender setHighlighted:YES];
}
I've tried using both [button setImage] and [button setBackgroundImage] but the same issue shows up. I've also tried setting the normal_state image to nil before setting isHighlighted to YES but no matter what the normal_state image stays and the highlight_state image gets drawn on top of it.
asked 3 mins ago
UIButton image not updating properly when changing states
Aucun commentaire:
Enregistrer un commentaire