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.
所以我正在创建一个UIButton并且我想将它设置为针对不同状态(正常和突出显示)具有两个单独的图像,但是当将突出显示的属性设置为是时,突出显示的图像将被绘制在正常状态图像之上,而不是完全取代它。
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.
我尝试过使用[button setImage]和[button setBackgroundImage],但同样的问题出现了。我还尝试将normal_state图像设置为nil,然后将设置isHighlighted设置为YES,但无论normal_state图像保留什么,highlight_state图像都在其上方绘制。
1 个解决方案
#1
0
CGFloat size = 50.0;
CGRect frame = CGRectMake(100, 100, size, size);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundColor:[UIColor clearColor]];
[button setFrame:frame];
[button setImage:[UIImage imageNamed:@"image1"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"image1"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:@"image1"] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
-(void)buttonPressed:(UIButton *)sender{
[sender setSelected:NO];
[sender setHighlighted:YES];
}
You can make button state one at a time i.e. Normal/Highlighted/Selected if you set two state i.e. [sender setSelected:YES];[sender setHighlighted:YES]; at one time. compiler automatically work according to last line i.e.[sender setHighlighted:YES] and make [sender setSelected:NO]; that why it is showing only Highlighted image on button.
如果设置了两个状态,即[发送者setSelected:YES]; [sender setHighlighted:YES],您可以一次创建一个按钮状态,即正常/突出显示/选择。在一次。编译器根据最后一行自动工作,即[sender setHighlighted:YES]并使[sender setSelected:NO];这就是为什么它只在按钮上显示突出显示的图像。
#1
0
CGFloat size = 50.0;
CGRect frame = CGRectMake(100, 100, size, size);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundColor:[UIColor clearColor]];
[button setFrame:frame];
[button setImage:[UIImage imageNamed:@"image1"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"image1"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:@"image1"] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
-(void)buttonPressed:(UIButton *)sender{
[sender setSelected:NO];
[sender setHighlighted:YES];
}
You can make button state one at a time i.e. Normal/Highlighted/Selected if you set two state i.e. [sender setSelected:YES];[sender setHighlighted:YES]; at one time. compiler automatically work according to last line i.e.[sender setHighlighted:YES] and make [sender setSelected:NO]; that why it is showing only Highlighted image on button.
如果设置了两个状态,即[发送者setSelected:YES]; [sender setHighlighted:YES],您可以一次创建一个按钮状态,即正常/突出显示/选择。在一次。编译器根据最后一行自动工作,即[sender setHighlighted:YES]并使[sender setSelected:NO];这就是为什么它只在按钮上显示突出显示的图像。