LoginSignup
9
7

More than 3 years have passed since last update.

[Swift 5]ImageViewの画像の色を変更するにはRenderingModeを変更しよう

Last updated at Posted at 2019-07-16

ものすごく簡単なことですが、いつもつい忘れてしまうので…

通常、デフォルトではUIImageのレンダリングモードがオリジナルモードとなっているため、以下のようにtintColorを指定しても、元の画像の色から変更できない。

iconView.image = UIImage(named: "imageName")
iconView.tintColor = .gray

色を変更するならレンダリングモードを変更する必要があります

UIImageを生成した時にwithRenderingMode()を繋げて指定し、レンダリングモードをテンプレートモードにすればOK!

iconView.image = UIImage(named: "imageName")?.withRenderingMode(.alwaysTemplate)
iconView.tintColor = .gray

alwaysTemplateは、Assetsの画像Inspectorで指定することもできます。

参考:https://stackoverflow.com/questions/12872680/changing-uiimage-color

9
7
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
9
7