Jack Li
Jack Li

Reputation: 33

Change Color of Image View Icons Swift

I am programming an app in XCode 7.3 for iOS 9.3, and have a menu with different icons for each cell. I am trying to make the icons white, as they are default black, but cell!.imageView?.tintColor() seems to have no effect. Is there another method I could use?

EDIT: Here's the code after @LychmanIT's suggestion:

  switch indexPath.section {

    case 0: cell?.imageView?.image = UIImage(imageLiteral: "stuff.png")
    case 1: cell?.imageView?.image = UIImage(imageLiteral: "otherstuff.png")
    case 2: cell?.imageView?.image = UIImage(imageLiteral: "menu.png")
    default: cell?.imageView?.image = UIImage(imageLiteral: "error.png")

    }

    cell!.imageView?.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)

    cell!.imageView?.tintColor = UIColor.whiteColor()

EDIT: I fixed the problem by doing this:

cell!.imageView?.image! = (cell!.imageView?.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate))!

Upvotes: 2

Views: 8984

Answers (3)

Mukesh Shakya
Mukesh Shakya

Reputation: 445

imageView.image!.imageWithRenderingMode(.alwaysTemplate)
imageView.tintColor = .red

OR you can use an extension below:

import UIKit

extension UIImage {

    func overlayImage(color: UIColor) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale)
        let context = UIGraphicsGetCurrentContext()

        color.setFill()

        context!.translateBy(x: 0, y: self.size.height)
        context!.scaleBy(x: 1.0, y: -1.0)

        context!.setBlendMode(CGBlendMode.colorBurn)
        let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
        context!.draw(self.cgImage!, in: rect)

        context!.setBlendMode(CGBlendMode.sourceIn)
        context!.addRect(rect)
        context!.drawPath(using: CGPathDrawingMode.fill)

        let coloredImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return coloredImage
    }

}

Upvotes: 3

gabbo
gabbo

Reputation: 491

In Swift 3 this method became

func withRenderingMode(_ renderingMode: UIImageRenderingMode) -> UIImage

Beside the method name, the solution proposed by LychmanIT didn't work for me because the return of withRenderingMode method needs to be reassigned to the image that is passed to the imageView like:

let newImage = theImageView.image?.withRenderingMode(.alwaysTemplate)
theImageView.image = newImage
theImageView.tintColor = .redColor

Regarding the question, it is also possible to run the .withRenderingMode method in the switch, like:

switch indexPath.section {
    case 0: cell?.imageView?.image = UIImage(imageLiteral: "stuff.png").withRenderingMode(.alwaysTemplate)
    case 1: cell?.imageView?.image = UIImage(imageLiteral: "otherstuff.png").withRenderingMode(.alwaysTemplate)
    case 2: cell?.imageView?.image = UIImage(imageLiteral: "menu.png").withRenderingMode(.alwaysTemplate)
    default: cell?.imageView?.image = UIImage(imageLiteral: "error.png").withRenderingMode(.alwaysTemplate)
    }

EDIT:

Additionally you can set the render mode of an image in Asset Catalog's Attribute Instector in the Utilities pane.

Upvotes: 6

LychmanIT
LychmanIT

Reputation: 725

Lets try this:

theImageView.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
theImageView.tintColor = UIColor.redColor()

Upvotes: 5

Related Questions