swiftuiswiftui-actionsheet

SwiftUI ActionSheet how can i change cancel color


I have an action sheet that has 5 elements and a cancel button. I am trying to make the cancel button color red but it is not working. I have seen this one SwiftUI ActionSheet different color for each action. The destructive button style makes the other tabs red but do not know how to make it red for the cancel tab. I have updated my cancel tab to the following but the color is still Blue, any suggestions would be great.

.cancel(Text("Cancel")
.font(.system(size: 40.0))
.foregroundColor(Color.red))

This below is my code for the ActionSheet:

.actionSheet(isPresented: $showLocationOptions) {
    ActionSheet(title: Text("Which city/town is this place in ?"), message: Text("Select a location"), buttons: [

    .default(Text(location1)) {  },
    .default(Text(location2)) {  },
    .default(Text(location3)) {  },
    .default(Text(location4)) {  },
    .default(Text(location5)) {  },
    .cancel(Text("Cancel")
    .font(.system(size: 40.0))
    .foregroundColor(Color.red))
    
    ])
}

Solution

  • No explicit way, but as workaround you can just use destructive style for explicitly named cancel button with nop action, like

    .actionSheet(isPresented: $showLocationOptions) {
        ActionSheet(title: Text("Which city/town is this place in ?"), message: Text("Select a location"), buttons: [
    
        .default(Text(location1)) {  },
        .default(Text(location2)) {  },
        .default(Text(location3)) {  },
        .default(Text(location4)) {  },
        .default(Text(location5)) {  },
        .destructive(Text("Cancel")){       // << keep as last
                // just nop - will be just closed
            }    
        ])
    }