總結(jié):當(dāng)button的狀態(tài)為selected的時(shí)候馍迄,高亮?xí)r對(duì)應(yīng)的狀態(tài)應(yīng)該是selected|highlighted捅儒,這與normal下高亮的狀態(tài)highlighted不同灰蛙。
以下是stackover上的提問圣贸,附上:
I've a UIButton and I set it with:
UIButton?*myButton?=?[UIButton?buttonWithType:UIButtonTypeCustom];
UIImage?*imageNormal?=?[UIImage?imageNamed:@"normal.png"];
UIImage?*imageNormalHover?=?[UIImage?imageNamed:@"normalHover.png"];
UIImage?*imageSelected?=?[UIImage?imageNamed:@"selected.png"];
UIImage?*imageSelectedHover?=?[UIImage?imageNamed:@"selectedHover.png"];
[myButton?setImage:imageNormal?forState:UIControlStateNormal];
[myButton?setImage:imageSelected?forState:UIControlStateSelected];
if?(boolVar)?{
[myButton?setSelected:YES];
[myButton?setImage:imageSelectedHover?forState:UIControlStateHighlighted];
}?else?{
[myButton?setImage:imageNormalHover?forState:UIControlStateHighlighted];
}
The issue is when the state is normal and I try to press the button I see correctly the image normalHover.png but when the state is selected and I try to press the button I see still normalHover.png and not the selectedHover.png. It seems that with UIButton I'm unable to change the highlighted image. Do you how to solve?
Answer:
You need to set the image for the UIControlStateSelected | UIControlStateHighlighted combined state:
[myButton?setImage:imageSelectedHover?forState:(UIControlStateSelected?|?UIControlStateHighlighted)];
Because both states are on when the button is selected and you hignlight it by tapping on it.
(uibutton選中狀態(tài)和高亮狀態(tài)的區(qū)別:選中可以常態(tài),高亮狀態(tài)在Down才有效于宙,up時(shí)候狀態(tài)無(wú)效)----按下未抬起時(shí)為高亮狀態(tài)