DEV Community

Saurabh Chavan
Saurabh Chavan

Posted on

1

model show on click in swiftUI

when we click on the button then new sheet will be come from the button thats called the model or basically we use the sheet() method to display the model over the screen

the code and output are as follow:

import SwiftUI

struct sheetView:View{
    @Environment(\.dismiss) var dismiss

    var body: some View{
        VStack{
            Button("Press to dismiss") {
                dismiss()
            }
            .font(.title)
            .padding()
            .background(.green)
            .cornerRadius(20)
            .foregroundColor(.white)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(.red)
    }
}

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

    var body: some View{

            Button("Show sheet") {
                showSheet = true
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
                .background(.yellow)
                .ignoresSafeArea()
            .sheet(isPresented: $showSheet) {
                sheetView()
            }
        }

}       

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Enter fullscreen mode Exit fullscreen mode

outPut:

Image description

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay