我有UIButton
並設置其背景圖像與正確的代碼。UIButton的形象與國家
`[button setImage:self.image forState:UIControlStateNormal]`
,它顯示了當按鈕被初始化,但是我想讓消失圖像時的狀態爲UIControlStateSelected
正確的圖像。
我可以改變圖像時,它被選中,但不知道哪個是正確的功能或值被設置爲沒有圖像時選擇。
我應該讓image alpha
爲0.0
?
謝謝!
我有UIButton
並設置其背景圖像與正確的代碼。UIButton的形象與國家
`[button setImage:self.image forState:UIControlStateNormal]`
,它顯示了當按鈕被初始化,但是我想讓消失圖像時的狀態爲UIControlStateSelected
正確的圖像。
我可以改變圖像時,它被選中,但不知道哪個是正確的功能或值被設置爲沒有圖像時選擇。
我應該讓image alpha
爲0.0
?
謝謝!
使用photoshop,創建透明背景的新形象。將其保存爲nope.png文件,將其拖到您的項目中。然後
[button setImage:[UIImage imageNamed:@"nope.png"] forState:UIControlStateSelected];
另一種方式是你需要創建新的圖像類別:
@interface UIImage (UIImageFrameColor)
+(UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size;
@end
@implementation UIImage (UIImageFrameColor)
+(UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, YES, 0);
[color set];
UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, size.width, size.height)];
[path fill];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
然後,你需要做一個形象有按鈕晚飯視圖的背景顏色相同的顏色
UIImage *bgImage = [UIImage imageWithColor:button.supperview.backgroundColor andSize:button.frame.size];
[button setImage:bgImage forState:UIControlStateSelected];
嘗試[button setImage:nil forState:UIControlStateSelected];
希望這有助於!
我試過這個,我猜它什麼時候它是零,它抓住了從StateNormal默認的一個。 – fkkcloud
嘗試[button setSeleted:YES];
如果你想創建一個按鈕,如檢查列表按鈕。這是一個示例代碼:
[button setImage:imageNormal forState:UIControlStateNormal];
[button setImage:imageSelected forState:UIControlStateSelected];
.....
-(void)btnButtonPressed:(UIButton*)sender {
[sender setSelected:!sender.selected];
}
希望這對你有所幫助!
我的問題是沒有得到不同的圖像時,它是在StateSelected,但使默認圖像完全透明或根本不存在。 – fkkcloud
請檢查並嘗試。這可能對你有用:
[button setImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
是的,我想這只是我可以實現它的方式.. – fkkcloud