Reputation: 61
I want to create action in .trailingSwipeActionsConfigurationForRowAt
with image in original image's colors. By default all image turns white. In my image there is few colors and I want to keep them all, so I can't just tint it. I'm trying to use .withRenderingMode(.alwaysOriginal)
but it doesn't seem to work at all - the image still turns white, I've tried pdf and png, all the same.
Here is my code:
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let isOn = UIContextualAction(style: .normal, title: "", handler: { _, _, completionHandler in
completionHandler(true)
})
isOn.backgroundColor = UIColor(red: 183/255, green: 189/255, blue: 196/255, alpha: 1)
isOn.image = UIImage(named: "SwitchOn")?.withRenderingMode(.alwaysOriginal)
return UISwipeActionsConfiguration(actions: [isOn])
}
Is there any way to make it work?
UPD:
I found only one way around it - use .backgroundColor = UIColor(patternImage: theImage)
instead of .image
.
But if you will use this trick, as I did in the end - please, notice that you will need to create an image with exact height as cell's height and with extended colour field on the right side of the image. It will help to keep image from repeating in visible on the screen area. And also, the text title will appear (while using .image
it is hidden), so if you don't need it, just put an empty string in it.
Code example:
let theImage: UIImage? = UIImage(named:"MyImage")
action.backgroundColor = UIColor(patternImage: theImage!)
Image example:
Upvotes: 2
Views: 956