美文网首页
在xib中设置UIButton选中状态下的Image

在xib中设置UIButton选中状态下的Image

作者: novice_Qin | 来源:发表于2020-05-06 10:37 被阅读0次

1.如果是用纯代码初始化的话,相信大家都知道使用以下两句代码设置:

        likeBtn.setImage(UIImage.init(named: "图片1"), for: UIControl.State.normal)

        likeBtn.setImage(UIImage.init(named: "图片2"), for: UIControl.State.selected)

2.下面讲解的是XIB(或者Storyboard)设置Button选中状态下的image:

这里是UIButton的默认状态, 看我图上红色标记部分:

Type : Custom

State Config : Default

Image : 019.png

2.下面是selected状态下的设置:

这里是UIButton的选中状态, 看我图上红色标记部分:

Type : Custom

State Config : selected

Image : 019-2.png

如果这样设置了没有反应,请注意一下在你点击button的方法里面是否加了这样一句代码:

 @IBActionfunclikeAction(_sender:UIButton) {

       //xib设置点击按钮切换图片没反应,添加这句话就可以实现

        sender.isSelected = !sender.isSelected

    }

相关文章

网友评论

      本文标题:在xib中设置UIButton选中状态下的Image

      本文链接:https://www.haomeiwen.com/subject/bqeightx.html