translation
www.hackingwithswift.com/books/ios-s…

Display multiple options with ActionSheet

SwiftUI offers Alert, which presents important notifications and supports one or two buttons, sheet(), which presents a new view on top of the current view, and ActionSheet: a replacement for Alert, which allows us to add more buttons.

Visually alerts and Action Sheets are very different: On iPhones, the Alert appears in the center of the screen and must be turned off by selecting a button, while the Action Sheets slide out from the bottom and contain multiple buttons that can be closed by clicking Cancel or clicking on areas other than the Action Sheet.

Action Sheets and Alerts share many of the same functionality except for differences in the appearance and number of buttons presented. Both are attached to the view hierarchy using the modifier — Alert with Alert () and Action Sheet with actionSheet() — and both are automatically displayed by SwiftUI when a condition is set. Both use the same button, Both have some built-in default button styles: default(), cancel(), and destructive().

To illustrate the use of Action Sheets, we first need a basic view that triggers some condition. For example, to display some text first, click on the text and change a Boolean value:

struct ContentView: View {@State private var showingActionSheet = false
    @State private var backgroundColor = Color.white

    var body: some View {
        Text("Hello, World!")
            .frame(width: 300, height: 300)
            .background(backgroundColor)
            .onTapGesture {
                self.showingActionSheet = true}}}Copy the code

Then comes the important part: we need to add another modifier to the text view to create and display the Action Sheet.

Like Alert (), actionSheet() Modifier takes two arguments: one that determines whether the Action Sheet is currently displayed, and one that provides a closure for the actionSheet to be displayed — usually in the form of a trailing closure.

We’re going to give the Action Sheet a title and a message, and then an array button. The buttons are arranged vertically in the order you provided them, and usually we put the Cancel button last — yes, you can turn it off by clicking elsewhere on the screen, but it’s best to leave an explicit option for the user.

Add this modifier to your text view:

.actionSheet(isPresented: $showingActionSheet) {
    ActionSheet(title: Text("Change background"), message: Text("Select a new color"), buttons: [
        .default(Text("Red")) { self.backgroundColor = .red },
        .default(Text("Green")) { self.backgroundColor = .green },
        .default(Text("Blue")) { self.backgroundColor = .blue },
        .cancel()
    ])
}Copy the code

Running the app, you should see that clicking on the text triggers the Action Sheet to slide in, and clicking on its button option causes the text background color to change.

                                



My official account here Swift and computer programming related articles, as well as excellent translation of foreign articles, welcome to pay attention to ~