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

jo *_*ion 4 tableview swipe swift

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

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

我尝试改变它的颜色但没有成功

请任何人都可以帮助我

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])
}
Run Code Online (Sandbox Code Playgroud)

小智 6

  1. 只需转到Assets.xcassets,选择所需的图像。

替换Render As为您需要的:

在此输入图像描述


添加

您还可以尝试一些事情:

  1. UIImageView更改您的内部的设置tableView
UIImageView.appearance(whenContainedInInstancesOf: [UITableView.self]).tintColor = UIColor.init(red: 239/255, green: 239/255, blue: 239/255, alpha: 1)
Run Code Online (Sandbox Code Playgroud)
  1. 尝试在以下位置使用扩展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
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用:

likeAction.image = UIImage(named: "Favourite_Selected")?.colored(in: .red)
Run Code Online (Sandbox Code Playgroud)

希望这篇文章能帮到你!