Image in TableViewCell swipe action

后端 未结 3 905
一向
一向 2020-12-12 02:10

I have a swipe action to \'complete\' a task in my to-do list app. This is the image I have set:

However when the cell is swiped, the image looks like this:

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-12 03:07

    In came up with this extension for UIImage:

        extension UIImage {
            func colored(in color: UIColor) -> UIImage {
                let renderer = UIGraphicsImageRenderer(size: size)
                return renderer.image { context in
                    color.set()
                    self.withRenderingMode(.alwaysTemplate).draw(in: CGRect(origin: .zero, size: size))
                }
            }
        }
    

    It returns a new icon in the new color when used like this:

            swipAction.image = UIImage(named: "myImage")?.colored(in: .white)
    

    Maybe this helps someone.

提交回复
热议问题