swiftswiftuiactionsheet

SwiftUI - What is the difference between sheet and actionSheet?


SwiftUI has these two modifiers:

.actionSheet(isPresented: $showActionPurchase) { () -> ActionSheet in

and

.sheet(isPresented: $showAlert,

one presents an action sheet and the other presents a sheet (?)

Why? What is the difference between these elements, if any?


Solution

  • sheet used for showing some view modal-way, but actionSheet is kind of alert view! they are very diffrent topic!

        import SwiftUI
    
    struct ContentView: View {
        
        @State var showSheet = false
        @State var showActionSheet = false
        
        let appActionSheet = ActionSheet(title: Text("ActionSheet"), message: Text("I am an ActionSheet"), buttons: [
            .default(Text("some text")),
            .cancel()
        ])
        
        
        var body: some View {
    
            VStack
            {
                
                Button("show sheet") {showSheet.toggle()}.padding()
                
                Button("show actionSheet") {showActionSheet.toggle()}.padding()
                
                
            }.font(Font.title.bold())
            .sheet(isPresented: $showSheet, content: {sheetView()})
            .actionSheet(isPresented: $showActionSheet, content: {appActionSheet})
    
        }
    }
    
    
    
    struct sheetView: View {
        var body: some View {
            
            ZStack
            {
                Color.red
                Text("I am sheet view")
            }.ignoresSafeArea()
        }
    }
    

    enter image description here

    enter image description here