2

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:

Image example

Lee B
  • 61
  • 6
  • Did you take a peek at this question? https://stackoverflow.com/q/44771778/4475605 I think you're on the right path with the rendering mode. Additionally, you might want to check your XCAssets to see if the rendering mode is what you want there, too. – Adrian Dec 25 '18 at 16:50
  • One other cool thing you can do if you've got a color swatch is instead of typing out RGB values, you can type `Color Literal` and it'll put a white box there...double click the white box, then click the eyedropper and you can click on the color you'd like to specify. You'll also be able to see the color. – Adrian Dec 25 '18 at 17:00
  • Checked this question, the best suggestion from its answers - to make image that has the same height as the cell and much bigger colour field on the right side and use it in `.backgroundColor`. Will work, but `.image` with working `withRenderingMode` would be much better. IF `withRenderingMode` would work... What do you mean to check XCAssets in that case? – Lee B Dec 25 '18 at 17:03
  • Oh, thank you, didn't know about Color Literal, that's nice. – Lee B Dec 25 '18 at 17:07
  • Guessing you probably store the images for your project in `Assets.xcassets`. In there, click your `SwitchOn` and then click the attributes inspector icon on the right side of the screen, then look at the rendering mode for it. – Adrian Dec 25 '18 at 17:21
  • 1
    Tried all of the modes in Assets -> Attribute Inspector -> Render as (Default, Original image, Template image), nothing changes - the image turns white anyway. – Lee B Dec 25 '18 at 19:22

0 Answers0