📜  swiftui actionsheet - Swift (1)

📅  最后修改于: 2023-12-03 15:35:12.917000             🧑  作者: Mango

SwiftUI ActionSheet

Introduction

The ActionSheet is a SwiftUI view that presents a list of actions that can be performed in response to a user interaction. It is usually triggered by tapping on a button or by a long press gesture. The ActionSheet view can be customized to include a title, message, and a set of actions that are displayed in a list.

Creating an ActionSheet

An ActionSheet can be created using the actionSheet modifier on a view. The actionSheet modifier takes a closure that returns an ActionSheet view. Here's an example:

struct ContentView: View {
    @State private var showingActionSheet = false

    var body: some View {
        Button(action: {
            self.showingActionSheet = true
        }) {
            Text("Show Action Sheet")
        }
        .actionSheet(isPresented: $showingActionSheet) {
            ActionSheet(title: Text("Select an Option"), message: Text("Choose one of the following options"), buttons: [
                .default(Text("Option 1")),
                .default(Text("Option 2")),
                .cancel(),
                .destructive(Text("Delete"))
            ])
        }
    }
}

In this example, we have a button that, when tapped, sets the showingActionSheet state variable to true. This variable is used to control the presentation of the ActionSheet. The actionSheet modifier takes an isPresented binding that is bound to the showingActionSheet state variable. It also takes a closure that returns an ActionSheet view. The ActionSheet view is initialized with a title, a message, and an array of Button views that represent the actions.

Customizing an ActionSheet

The ActionSheet view can be customized by changing its title, message, and actions. Here are some examples:

Changing the Title
.actionSheet(isPresented: $showingActionSheet) {
    ActionSheet(title: Text("Select an Option"), message: Text("Choose one of the following options"), buttons: [
        .default(Text("Option 1")),
        .default(Text("Option 2")),
        .cancel(),
        .destructive(Text("Delete"))
    ])
}

In this example, we have set the title of the ActionSheet to Select an Option.

Changing the Message
.actionSheet(isPresented: $showingActionSheet) {
    ActionSheet(title: Text("Select an Option"), message: Text("Choose one of the following options"), buttons: [
        .default(Text("Option 1")),
        .default(Text("Option 2")),
        .cancel(),
        .destructive(Text("Delete"))
    ])
}

In this example, we have set the message of the ActionSheet to Choose one of the following options.

Adding an Action
.actionSheet(isPresented: $showingActionSheet) {
    ActionSheet(title: Text("Select an Option"), message: Text("Choose one of the following options"), buttons: [
        .default(Text("Option 1")),
        .default(Text("Option 2")),
        .cancel(),
        .destructive(Text("Delete")),
        .default(Text("Option 3"))
    ])
}

In this example, we have added an additional action to the ActionSheet.

Conclusion

The ActionSheet is a very useful view for presenting a set of actions to a user in a customizable, user-friendly way. By using the actionSheet modifier on a view, you can easily display an ActionSheet view and customize its appearance to suit your needs.