Swift 如何更改Tableview的UIContextualAction的图像着色颜色?

Swift 如何更改Tableview的UIContextualAction的图像着色颜色?,swift,tableview,swipe,Swift,Tableview,Swipe,在那个里,我想改变图像的色调,但无法改变,它总是返回白色。我已尝试渲染原始图像和模板图像,但仍然无法工作 我在下面添加了屏幕截图: 请任何人都能帮我 public func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { guard let cell = tab

在那个里,我想改变图像的色调,但无法改变,它总是返回白色。我已尝试渲染原始图像和模板图像,但仍然无法工作

我在下面添加了屏幕截图:

请任何人都能帮我

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])
}
  • 只需转到
    Assets.xcsets
    ,选择所需图像
  • 渲染为
    替换为所需的:


    附加 您还可以尝试以下几点:

  • 表视图中更改
    UIImageView
    本身的设置:
  • 尝试在
    UIImage
    上使用扩展名:
  • 如何使用:

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

    希望这个能帮助你

    要更改图像颜色,必须为背景色设置alpha 0

    像这条线:


    likeAction.backgroundColor=UIColor.init(红色:239/255,绿色:239/255,蓝色:239/255,alpha:0)

    我已经尝试过将Rendring作为模板和原始格式,但不会尝试work@josolution,我补充了我的答案,请检查@kzakharc我已经尝试了以上所有的解决方案,但都不起作用。渲染为originalImage为我做了这个把戏。扩展成功了@Maitree swift我已经尝试过了,但不起作用
    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
        }
    }
    
    likeAction.image = UIImage(named: "Favourite_Selected")?.colored(in: .red)