I've implemented the following method below and while the functionality works the image displayed is completely white in color. Is there a property within the editing style that I need to change?
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { _, _, completion in
tableView.beginUpdates()
self.viewModel.remove(atIndex: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
completion(true)
}
deleteAction.image = UIImage(named:"trash-can") // Image is red in Assets but here it's completely white
deleteAction.backgroundColor = UIColor(named: "Navy") ?? .blue
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
return configuration
}
Thanks
CodePudding user response:
So I managed to find the answer and it's down to two things. First, the view hierarchy showed a white tint applied to the image after the fact, which couldn't be manipulated. Second, in order to solve the issue, the image needed to be created using CGImage and wrapping it in an overriding rendering mode class.
Here's the working code. Also when implementing, pay attention to the scale factor - In my case, I needed the image to be half the width of the view and needed a scale of 1.5, not 0.5 as you would naturally add :)
UIImage wrapper class:
class OriginalImageRender: UIImage {
override func withRenderingMode(_ renderingMode: UIImage.RenderingMode) -> UIImage {
return self
}
}
Implementation:
let cgImageX = UIImage(named: "trash-can")?.cgImage
deleteAction.image = OriginalImageRender(cgImage: cgImageX!, scale: 1.5, orientation: .up)
deleteAction.backgroundColor = UIColor(named: "Navy") ?? .blue
CodePudding user response:
UIImage(named: "trash-can").withRenderingMode(.alwaysOriginal)