4

There I want to change tint color of image but couldn't change, it's always returning white color. I have try Rendring image of Original and Template image but still it won't work.

I have added screen shot below:

I have try to change it's color but not sucessed

please anyone can help me for that

public func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    guard let cell = tableView.cellForRow(at: indexPath) as? DocumentListCell else {return UISwipeActionsConfiguration()}
    let likeAction = UIContextualAction(style: .normal, title:  "", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        print("likeAction ...")
        success(true)

    })

    likeAction.image = #imageLiteral(resourceName: "Favourite_Selected")
    likeAction.backgroundColor = UIColor.init(red: 239/255, green: 239/255, blue: 239/255, alpha: 1)

    let downloadAction = UIContextualAction(style: .normal, title:  "", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        print("Trash action ...")
    })
    downloadAction.backgroundColor = UIColor.red//UIColor.init(red: 239/255, green: 239/255, blue: 239/255, alpha: 1)
    downloadAction.image = #imageLiteral(resourceName: "Doc_Download_Big")

    return UISwipeActionsConfiguration(actions: [likeAction,downloadAction])
}
norbitrial
  • 14,716
  • 7
  • 32
  • 59
jo solution
  • 401
  • 5
  • 18

2 Answers2

6
  1. Just go to Assets.xcassets, select the desired image.

Replace Render As with the one you need:

enter image description here


Addition

You can try also a few things:

  1. Change the settings of the UIImageView itself inside your tableView:
UIImageView.appearance(whenContainedInInstancesOf: [UITableView.self]).tintColor = UIColor.init(red: 239/255, green: 239/255, blue: 239/255, alpha: 1)
  1. Try to use extension on UIImage:
extension UIImage {
    
    func paintOver(with color: UIColor) -> UIImage {
        let renderer = UIGraphicsImageRenderer(size: size)
        let renderedImage = renderer.image { _ in
            color.set()
            self.withRenderingMode(.alwaysTemplate).draw(in: CGRect(origin: .zero, size: size))
        }
        
        return renderedImage
    }
}

How to use:

likeAction.image = UIImage(named: "Favourite_Selected")?.colored(in: .red)

Hope this one will help you!

Community
  • 1
  • 1
kzakharc
  • 499
  • 2
  • 8
0

You have to set alpha zero for background color for change image color.

Like this line:-

likeAction.backgroundColor = UIColor.init(red: 239/255, green: 239/255, blue: 239/255, alpha: 0)