button点击切换背景图片的问题

偶尔善良 提交于 2019-12-06 04:42:26

我做了个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];

    }
这样就可以了,具体还没知道原因!!,有知道的请多多指点哈

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!