5

I have a question regarding Actionsheet in SwiftUI. I want to create an ActionSheet with 2 options: delete and cancel. "Delete" button colored in red and "cancel" in green.

Here is an example of code:

Button(action: {
                    print("Delete button pressed")
                    self.showingActionSheet = true
                }){
                    Text("Go to actions")
                        .foregroundColor(.green)
                        .font(.body)
                        .padding()
                }
                .actionSheet(isPresented: $showingActionSheet) {                   
                    return ActionSheet(title: Text("Delete images"), buttons: [
                        .default(Text("Delete selected").foregroundColor(.red)){
                            // some action to do
                        },
                        .cancel()
                    ])
                }

The problem is that the color for actions is the default one ("blue") for both buttons. I can change this by adding the following line in "SceneDelegate.swift" or even in the code above.

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor(named: "green")

The problem with this line is that it will overwrite the general color from "blue" to "green". Still need to find a solution on how to color each action differently.

This is how it looks like: image preview

Do you have any sugestions?

1 Answers1

4

There is another button style for actions like delete

demo

.actionSheet(isPresented: $showingActionSheet) {
    return ActionSheet(title: Text("Delete images"), buttons: [
        .destructive(Text("Delete selected")){
            // some action to do
        },
        .cancel()
    ])
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Thank you for the response. It's working well in the case presented by me. But in case I want another color apart from from red (let's say purple) is there a posibility to do this? – Theodor Ungureanu Jul 20 '20 at 13:28