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

标签 swift tableview swipe

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

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

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

请任何人都可以帮助我

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])
}

最佳答案

  1. 只需前往 Assets.xcassets,选择所需的图像。

Replace Render As with the one you need:

enter image description here

<小时/>

添加

您还可以尝试一些方法:

  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
    }
}

使用方法:

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

希望这篇文章能对您有所帮助!

关于swift - 如何更改 Tableview 的 UIContextualAction 的图像色调颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58957481/

相关文章:

ios - 滑动返回不适用于 iOS 应用

swift - 如何在 Swift 4 中获取 AVPlayer 项目的总持续时间

macos - 滚动时 NSTableView 非常滞后

java - 在javafx中填充tableview

IOS swift tableview获取一个部分的headerview

带有 TableView 的 Swift 4 SegmenterControl

iphone - 滑动即可更改 View

ios - 在 Swift 中上传 Google Drive 上的电子表格文件

swift - 为什么 iOS SpriteKit 场景/ View 坐标在 iOS 7.1 和 iOS 8 之间不同?

xcode - 滑动长行的操作按钮位置