How to Use SwiftUI Sheet – Tutorial and Examples

SwiftUI Sheets present modals that slide up, displaying new content without affecting navigation. This guide covers creating and presenting sheets in SwiftUI.
Written by

Joash Tubaga

Updated on

Aug 23 2024

Table of contents

    Overview

    In SwiftUI, Sheets are used to present modals that slide up from the bottom of the screen. They provide a way to present new content without pushing a new view onto the navigation stack. Sheets are commonly used for presenting additional information, forms, or any other content that temporarily takes over part of the screen. This article will walk you through creating and presenting a simple sheet using SwiftUI.

    Code Snippet

    Swift
    import SwiftUI
    
    struct ContentView: View {
        @State private var showingSheet = false // State to control sheet presentation
        
        var body: some View {
            VStack {
                Button("Show Sheet") {
                    showingSheet.toggle() // Toggle the state to show the sheet
                }
            }
            .sheet(isPresented: $showingSheet, content: {
                SheetView() // Content of the sheet
            })
            .padding()
        }
    }
    
    struct SheetView: View {
        @Environment(\.dismiss) var dismiss
        
        var body: some View {
            VStack {
                Text("This is a sheet")
                    .font(.largeTitle)
                    .padding()
                
                Button("Dismiss") {
                    dismiss()
                }
            }
        }
    }

    Code Explanation

    • @State private var showingSheet = false: This line declares a state variable that controls whether the sheet is presented. It is initialized to false because we do not want the sheet to be visible initially.
    • Button("Show Sheet") { showingSheet.toggle() }: This button toggles the showingSheet state variable. When pressed, it changes the state from false to true, thereby presenting the sheet.
    • .sheet(isPresented: $showingSheet, content: { SheetView() }): This modifier attaches a sheet to the view. It takes a binding to showingSheet, meaning the sheet will be presented when showingSheet is true and dismissed when it is false. The closure { SheetView() } defines the content of the sheet.
    • @Environment(\.dismiss) var dismiss: This line declares an environment variable to handle the dismissal of the sheet.
    • Button("Dismiss") { dismiss() }: This button calls the dismiss function to close the sheet when pressed.

    Sheets in SwiftUI provide a straightforward way to present modals. They are highly customizable and can contain any SwiftUI view, making them versatile for various use cases. Whether you need to present a simple message or a complex form, Sheets in SwiftUI offer a clean and efficient solution for your app’s user interface.



    Get started for free

    Join over 2,000+ students actively learning with CodeWithChris