Showing a sheet modally
The Modality design technique can be used to present content in a temporary mode that's separate from the user's previous content and requires an explicit action to exit. Modals can help focus on a self-contained task or set of closely related tasks.
In this recipe, we will learn how to present a sheet modally and how to add navigation items to the modal.
Getting ready
Create a SwiftUI app named ModalViewApp
.
How to do it
We shall create two SwiftUI views named ModalView
and ModalWithNavView
that would be displayed modally when a button is clicked. The steps are as follows:
- In
ContentView.swift
, between the struct declaration and the body variable, add two state variables,showModal
andsheetWithNav
. The state variables will be used to trigger the sheet presentation:@State private var showModal = false @State private var sheetWithNav = false;
- Add a
VStack
and a button that changes the value of ourshowModal
variable totrue...