1

I'm having some issues initializing an image in my code. I'm trying to change its tintColor property when the cell it sits in is highlighted like so:

iconImageView.tintColor = isHighlighted ? UIColor.white : UIColor.black

And to do so I'm initializing the image with the following line:

iconImageView.image = UIImage(named: imageName)?.renderingMode(.alwaysTemplate)

But I'm getting the following error:

Cannot call value of non-function type 'UIImage.RenderingMode'

Any suggestions?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Hey, try with this link https://stackoverflow.com/questions/24145221/creating-uiimage-with-renderingmode-in-swift – Vivek Oct 25 '18 at 04:43

2 Answers2

1

The 'renderingMode' is a read-only property of UIImage.

Creates and returns a new image object with the specified rendering mode. You can use:

open func withRenderingMode(_ renderingMode: UIImage.RenderingMode) -> UIImage

Code snippet:

let imageView: UIImageView = UIImageView.init()
let image = UIImage.init(named: "name")?.withRenderingMode(.alwaysOriginal)
imageView.image = image
Tiny
  • 56
  • 1
0

renderingMode specifies the possible rendering modes for an image.

withRenderingMode(_:) creates and returns a new UIImage object with the specified rendering mode.

So, you need to use withRenderingMode(_:) which is a function versus renderingMode which is a property.

let image = UIImage(named: "imageName")?.withRenderingMode(.alwaysTemplate)
iconImageView.image.setImage(image, for: .normal)
iconImageView.image.tintColor = isHighlighted ? UIColor.white : UIColor.black
Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
Sabbir Ahmed
  • 351
  • 1
  • 13