美文网首页iOS 开发技巧
如何在 UIButton 被按下时改变背景颜色?

如何在 UIButton 被按下时改变背景颜色?

作者: 张嘉夫 | 来源:发表于2017-03-07 20:03 被阅读325次

    覆写 UIButton 即可:

    - (void) setHighlighted:(BOOL)highlighted {
        [super setHighlighted:highlighted];
    
        if (highlighted) {
            self.backgroundColor = UIColorFromRGB(0x387038);
        }
        else {
            self.backgroundColor = UIColorFromRGB(0x5bb75b);
        }
    }
    

    Swift 3.0

    override var isHighlighted: Bool {
        didSet {
            switch isHighlighted {
            case true:
                backgroundColor = UIColor.white
            case false:
                backgroundColor = UIColor.black
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:如何在 UIButton 被按下时改变背景颜色?

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