我做了个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]; }
这样就可以了,具体还没知道原因!!,有知道的请多多指点哈
时间: 2024-10-19 20:20:16