標籤:ios開發 uibutton 圖片
我做了個UIButton的擴充,代碼如下:
#import <UIKit/UIKit.h>@interface CheckBoxButton : UIButton@property (nonatomic,assign) BOOL isChecked;@end
我通過下面代碼來實現點擊後切換圖片,但是並不能達到效果
if (button.isChecked) { self.collectionButton.imageView.image = [UIImage imageNamed:@"shouchang_do"]; }else { self.collectionButton.imageView.image = [UIImage imageNamed:@"shouchang_undo"]; }
這樣設定後,點擊按鈕,圖片並沒有變換,解決辦法:
if (button.isChecked) { //self.collectionButton.imageView.image = [UIImage imageNamed:@"shouchang_do"]; [self.collectionButton setImage:[UIImage imageNamed:@"shouchang_do"] forState:UIControlStateNormal]; }else { //self.collectionButton.imageView.image = [UIImage imageNamed:@"shouchang_undo"]; [self.collectionButton setImage:[UIImage imageNamed:@"shouchang_undo"] forState:UIControlStateNormal]; }這樣就可以了,具體還沒知道原因!!,有知道的請多多指點哈
button點擊切換背景圖片的問題