Present and Dismiss a sheet in SwiftUI

Ronak Patel
1 min readMay 28, 2021

A sheet in iOS is a system provided view that appears modally on top of any other currently displayed view.
A sheet is by default empty, we need to provide it with custom views and content.

Instance method: .sheet(isPresented:onDismiss:content:)

Parameters:

isPresentedA binding to a Boolean value that determines whether to present the sheet that you create in the modifier’s content closure.

onDismissThe closure to execute when dismissing the sheet.

contentA closure that returns the content of the sheet.

import SwiftUI
struct SheetView: View {
@State private var isShowingSheet = false
var body: some View {
Button(action: {
isShowingSheet.toggle()
})
{
Text("Show Sheet View")
}
.sheet(isPresented: $isShowingSheet,
onDismiss:didDismiss){
VStack {
Text("Welcome...")
.font(.title)
.padding(50)
Text("""
This is a Sheet View.
""")
.padding(50)
Button("Dismiss the sheet",action: {
isShowingSheet.toggle()
})
}
}
}
func didDismiss() {
// Handle the dismissing action.
}
}
struct SheetView_Previews: PreviewProvider {
static var previews: some View {
SheetView()
}
}

Now, Call this SheetView() in the ContentView().

import SwiftUI
struct ContentView: View {
var body: some View {
SheetView()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Sheet with full screen cover

Presents a modal view that covers as much of the screen as possible when a given condition is true.

If you want that a sheet should cover full screen then it’s pretty easy to do; instead of using the sheet(isPresented:content:) view modifier, simply use the fullScreenCover(isPresented:content:)

--

--