Swift and SwiftUI tutorials for Swift Developers

How to show multiple options with ConfirmationDialog() in SwiftUI

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:

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Article

How to show an Action Sheet in SwiftUI

Next Article

What's and how to use the gesture modifier in SwiftUI

Related Posts