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

时间:2019-11-20 15:06:17

标签: swift tableview swipe

我想更改图像的色彩,但不能更改,它总是返回白色。我尝试使用Rendring原始图像和模板图像,但仍然无法使用。

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

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

2 个答案:

答案 0 :(得分:0)

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

Render As替换为您需要的一个:

enter image description here


加法

您还可以尝试一些操作:

  
      
  1. UIImageView内更改tableView本身的设置:
  2.   
UIImageView.appearance(whenContainedInInstancesOf: [UITableView.self]).tintColor = UIColor.init(red: 239/255, green: 239/255, blue: 239/255, alpha: 1)
  
      
  1. 尝试在UIImage上使用扩展名:
  2.   
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)

希望这对您有帮助!

答案 1 :(得分:0)

您必须将背景色的alpha值设置为零才能更改图像颜色。

喜欢这行:-

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