Ios 更改图像视图图标的颜色

Ios 更改图像视图图标的颜色,ios,swift,cocoa-touch,Ios,Swift,Cocoa Touch,我正在用XCode 7.3为iOS 9.3编写一个应用程序,每个单元都有一个带有不同图标的菜单。我正在尝试将图标设置为白色,因为它们默认为黑色,但是cell!。imageView?.tintColor()似乎没有效果。还有别的方法可以用吗 编辑:以下是@LychmanIT建议后的代码: switch indexPath.section { case 0: cell?.imageView?.image = UIImage(imageLiteral: "stuff.png")

我正在用XCode 7.3为iOS 9.3编写一个应用程序,每个单元都有一个带有不同图标的菜单。我正在尝试将图标设置为白色,因为它们默认为黑色,但是
cell!。imageView?.tintColor()
似乎没有效果。还有别的方法可以用吗

编辑:以下是@LychmanIT建议后的代码:

  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()
编辑:我通过以下操作修复了问题:

cell!.imageView?.image! = (cell!.imageView?.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate))!
让我们试试这个:

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

在Swift 3中,该方法成为

func withRenderingMode(_ renderingMode: UIImageRenderingMode) -> UIImage
除了方法名称之外,提出的解决方案对我不起作用,因为需要将withRenderingMode方法的返回重新分配给传递给imageView的图像,如:

let newImage = theImageView.image?.withRenderingMode(.alwaysTemplate)
theImageView.image = newImage
theImageView.tintColor = .redColor
关于这个问题,也可以在交换机中运行.withRenderingMode方法,如:

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)
    }
编辑:

此外,您还可以在实用程序窗格中的资源目录属性说明中设置图像的渲染模式

imageView.image!.imageWithRenderingMode(.alwaysTemplate)
imageView.tintColor = .red
或者,您可以使用以下扩展:

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
    }

}

您需要使用AlwaysTemplate渲染模式。像这样做有什么好处吗?或者它是不必要的?我认为有太多多余的代码。但不管怎么说,它起作用了:)@LychmanIT不知什么原因,它对我不起作用。我把我的代码放在一个问题中:你能看一下吗?我用上面的代码修复了它,因为
imageWithRenderingMode
返回所需的图像