LoginSignup
25
22

More than 5 years have passed since last update.

[Objective-C] UIImageの画像に色をつける

Posted at

Tab barなどアイコンに色を塗って表示することがあると思います。
iOS7からはUIImage#imageWithRendaringModeを変更して手軽にアイコンに色を塗ることが出来ます。

// UIImageを作る
UIImage *aImage = [UIImage imageNamed:@"icon.png"];

// ※ `imageWithRenderingMode:`メソッドを使って新しく`UIImage`を生成する
aImage = [aImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *aImageView = [[UIImageView alloc] initWithImage:aImage];

// アイコンに指定したい色を`tintColor`として設定
aImageView.tintColor = UIColor.redColor;
[self.view addSubview:aImageView];

以上のようにすることで手軽に色を設定できます。
一点注意点として、imageWithRenderingMode:メソッドは新しくUIImageを生成しているので、それをUIImageViewに設定しないと色が変わらないので注意です。

(最初、たんなる設定項目かと思って色が変わらずに焦った( ;´Д`))

25
22
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
25
22