LoginSignup
2
4

More than 5 years have passed since last update.

Swift UIButton の状態を1枚の画像で切り替える

Last updated at Posted at 2019-04-12

selected や highlighted の表示を 1枚の画像で切り替えたい場合のメモ

// normal時の画像をテンプレートとしてレンダリングモードで取得
let highlightedImage: UIImage = UIImage(named: "icon")!.withRenderingMode(.alwaysTemplate)

// その画像をハイライト時の画像にする
self.button.setImage(highlightedImage, for: .highlighted)

// tintColorで色を指定する
self.button.tintColor = UIColor.red

ボタンを押した時に同じ画像で色が赤になります。
わざわざ画像を作ってもらわなくても大丈夫。

2
4
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
2
4