1

I have a UIImageView in my app that displays a static image. The image is set as always template in the asset manager. Here is the inspector when capturing the interface during run-time:

The inspector when selecting the image view.

It seems that the tintColor is being set, but it's not being applied (or something??). Here is the accessibility information from the same panel as above:

The accessibility information

I attempted to fix it using this question, but it does not change the appearance. Here's the code I'm using (nested in layoutSubview):

// mode is always set to `.alwaysTemplate` and the below branch
// never executes.
let mode = promptImageView?.image?.renderingMode
if mode != .alwaysTemplate {
    if let image = promptImageView?.image {
        let newImage = image.withRenderingMode(.alwaysTemplate)
        promptImageView!.image = newImage
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Thomas Ring
  • 171
  • 1
  • 12

5 Answers5

1

It seems to be a bug between Interface Builder and the code. When setting the tintColor to the same color, more than once, it does not change to the target color at runtime. Here I've fixed it by changing the color in Interface Builder because .tintColor is set before the view appears.

Fixed by changing the color in the .xib file to a color other than the intended one. Here I've changed it from white to brown:

enter image description here

Thomas Ring
  • 171
  • 1
  • 12
0

Try this code and remove all other condition check if not required

imgBG.image = imgBG.image!.withRenderingMode(.alwaysTemplate)
imgBG.tintColor = UIColor.red
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
0

You should remove condition and generate image with instance & provide tint color what you want to set.

 if let image = promptImageView?.image {
    let newImage = image.withRenderingMode(.alwaysTemplate)
    promptImageView!.image = newImage
    self.promptImageView.tintColor = .red
}
Rakesh Patel
  • 1,673
  • 10
  • 27
0
self.image!.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
self.image.tintColor = UIColor.red

This reflection only shown on the output while run, when you capture the page static image only display

Vicky_Vignesh
  • 584
  • 2
  • 14
Nandhini
  • 564
  • 4
  • 8
0

Use below code to set image tint color.

extension UIImage
{

    func tintWithColor(_ color:UIColor)->UIImage
    {

        UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale);
        //UIGraphicsBeginImageContext(self.size)
        let context = UIGraphicsGetCurrentContext()

        // flip the image
        context?.scaleBy(x: 1.0, y: -1.0)
        context?.translateBy(x: 0.0, y: -self.size.height)

        // multiply blend mode
        context?.setBlendMode(.multiply)

        let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
        context?.clip(to: rect, mask: self.cgImage!)
        color.setFill()
        context?.fill(rect)

        // create uiimage
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage!

    }

}

Use it like:

imageView.image = UIImage(named: "image")?.tintWithColor(color)
Viral Savaliya
  • 180
  • 3
  • 5