๋””์ž์ด๋„ˆ์™€ ์ผ์„ ํ•˜๋‹ค๋ณด๋ฉด, image์˜ ์ƒ‰์ƒ ์ž์ฒด๋ฅผ ๋ณ€๊ฒฝํ•ด์ฃผ๊ธธ ๋ฐ”๋ผ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค. ์–ด๋–ป๊ฒŒ ํ•  ์ˆ˜ ์žˆ์„๊นŒ?

Answer

let image = UIImage(named: "asset_name")
    .withRenderingMode(.alwaysTemplate)
    .withTintColor(.red)

Tint Color

  • ์‹œ๊ฐ์ ์œผ๋กœ ํ™”๋ฉด์ƒ์˜ ์–ด๋–ค ์š”์†Œ๊ฐ€ ํ˜„์žฌ ํ™œ์„ฑํ™” ๋˜์—ˆ๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์š”์†Œ
  • Button ํด๋ฆญํ–ˆ์„ ๋•Œ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ๋ณ€ํ•˜๋ฉด์„œ ๋ˆŒ๋ ธ๋‹ค๋Š” ๊ฒƒ์„ ์ธ์ง€ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋•Œ ์ด๋Ÿฌํ•œ ํšจ๊ณผ๋ฅผ ๊ฐ€๋Šฅ์ผ€ํ•˜๋Š” ๊ฒƒ์ด tint color
  • tintcolor๋Š” UIView์˜ property๋กœ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ƒ์†๋ฐ›๋Š” ๋ทฐ์—์„œ ์ ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.
  • view.tintColor = UIColor.red
  • image์˜ ๊ฒฝ์šฐ์—๋Š” ์•ฝ๊ฐ„ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์œ„์— ์ ์–ด๋‘์—ˆ๋‹ค.

RenderingMode

  • automatic
    • Draw the image using the contextโ€™s default rendering mode.
    • context: ์–ด๋–ค UI Component์— ๋“ค์–ด๊ฐ„ ๊ฒƒ์ธ๊ฐ€?
    • UIBarButtonItem, UITabBarItem์— ์ด๋ฏธ์ง€๊ฐ€ ๋“ค์–ด๊ฐ„ ๊ฒฝ์šฐ์™€ UIImageView์— ๋“ค์–ด๊ฐ„ ๊ฒฝ์šฐ์— ๋ณด์ด๋Š” ์ด๋ฏธ์ง€๊ฐ€ ๋‹ฌ๋ž๋‹ค.
    • ์ด๋Ÿฐ ์˜๋„์—์„œ UIImage์˜ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ์Œ
  • alwaysOriginal
    • Always draw the original image, without treating it as a template.
    • image๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์›๋ž˜ ์ด๋ฏธ์ง€๋กœ ๊ทธ๋ ค์ง
  • alwaysTemplate
    • Always draw the image as a template image, ignoring its color information.
    • ๋ถˆํˆฌ๋ช…ํ•œ ๋ถ€๋ถ„์„ tint color๋กœ ๋Œ€์ฒดํ•จ

Reference