LoginSignup
8
8

More than 5 years have passed since last update.

UIButtonのhighlighted時に背景色を変更できる拡張クラス

Posted at
class UIButtonExt: UIButton {
    var defaultBackgroundColor: UIColor = UIColor.whiteColor()

    var highlightedBackgroundColor: UIColor = UIColor.whiteColor()

    override var highlighted: Bool {
        didSet {
            if (highlighted) {
                defaultBackgroundColor = backgroundColor!
                backgroundColor = highlightedBackgroundColor
            } else {
                backgroundColor = defaultBackgroundColor
            }
        }
    }
}

highlightedの変更を検知して、背景色を変更します。

「storyboardで色を適用させたい」という要望があれば、L4に@IBInspectableを付けておけば実現可能ですね

あとは好みによってはextensionで適用させてもいいかもしれませんね

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8