简体   繁体   English

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

[英]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. 我正在为iOS 9.3的XCode 7.3编写一个应用程序,并为每个单元格提供一个带有不同图标的菜单。 I am trying to make the icons white, as they are default black, but cell!.imageView?.tintColor() seems to have no effect. 我试图让图标变白,因为它们是默认的黑色,但是cell!.imageView?.tintColor()似乎没有效果。 Is there another method I could use? 我可以使用另一种方法吗?

EDIT: Here's the code after @LychmanIT's suggestion: 编辑:这是@ 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()

EDIT: I fixed the problem by doing this: 编辑:我通过这样做解决了这个问题:

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

Lets try this: 让我们试试这个:

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

In Swift 3 this method became 在Swift 3中,这种方法变成了

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: 除了方法名称之外, LychmanIT提出的解决方案对我不起作用,因为需要将withRenderingMode方法的返回重新分配给传递给imageView的图像,如:

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: 关于这个问题,也可以在交换机中运行.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)
    }

EDIT: 编辑:

Additionally you can set the render mode of an image in Asset Catalog 's Attribute Instector in the Utilities pane. 此外,您可以在“ 实用工具”窗格的“ 资产目录 ”的“ 属性检测器”中设置图像的渲染模式。

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
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM