In iOS 15 or higher SwiftUI allows us to select a list of options using the confirmationDialog() modifier which also works for macOS.
To create an action sheet using confirmationDialog(), you have to provide a text title, a binding to determine when we want to show our sheet and optionally if you want the text title to appear, if you don’t specify it SwiftUI will decide based on the context.
Here is an example of the confirmationDialog() modifier to show an action sheet.
struct ContentView: View {
@State private var showingOptions = false
@State private var selection = "None"
var body: some View {
VStack {
Text(selection)
Button("Confirm paint color") {
showingOptions = true
}
.confirmationDialog("Select a color", isPresented: $showingOptions, titleVisibility: .visible) {
Button("Red") {
selection = "Red"
}
Button("Green") {
selection = "Green"
}
Button("Blue") {
selection = "Blue"
}
}
}
}
}
Which in XCode would result in:
