Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

ios - Image in TableViewCell swipe action

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

enter image description here

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

Swiped image

Is there a way to make the background transparent/white and the image to have the green circle like the image above?

If there is not, is there a way to make the green background 'wrap' to the purple of the cell so there is no whitespace? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...