Set image color in SwipeAction

1.7k Views Asked by At

I'm trying to customise color of icon in swipeAction of SwiftUI. Outside foregroundColor() it works, inside, - not.

            Button {
                print("Hello")
            } label: {
                Label(
                    title: {},
                    icon: {
                        Image(systemName: "checkmark")
                            .foregroundColor(.purple) // It makes checkmark red
                    }
                )
            }

            if #available(iOS 15.0, *) {
                Text("Text").swipeActions {
                    Button {
                        print("Hello")
                    } label: {
                        Label(
                            title: {},
                            icon: {
                                Image(systemName: "checkmark")
                                    .foregroundColor(.purple) // It doesn't change anything
                            }
                        )
                    }
                }
            }
2

There are 2 best solutions below

2
vacawama On

A swipe action has its own styling that is applied by the system. According to the documentation:

For labels or images that appear in swipe actions, SwiftUI automatically applies the fill symbol variant

You can add color by adding the .tint() modifier.

Text("Text").swipeActions {
    Button {
        print("Hello")
    } label: {
        Label("Choose", systemImage: "checkmark")
    }
    .tint(.green)
}

Showing tint color in simulator


Note: The color red is used by default for destructive actions (like delete) and should be avoided for non-destructive actions. If your action is destructive, use Button(role: .destructive) and the system will color it red if you don’t apply .tint().

Check the documentation for more information about swipeActions.

0
Anessence On

Here is the way how I did it, but it is a workaround:

You can use UIImage instead of Image, tint it as alwaysOriginal and convert to Image again. Here is the basic code example:

 Image(uiImage: UIImage(named:"your_image")!.withTintColor(.purple, renderingMode: .alwaysOriginal))

P.S. It will work also with colors from your palette which may support light/dark mode switching, etc.

EDIT: I used code above instead of Label