We will create a half-modal.
Standard modal
import SwiftUI
struct ContentView: View
@State private var isModalView = false
var body: some View
Button("button")
isModalView = true
.sheet(isPresented: $isModalView)
ModalView()
struct ModalView: View
var body: some View
Text("This is modal view.")
In the code above, pressing the button labeled "button" displays a modal screen that covers the entire screen. This time, I want to display a modal screen that is half the size.
Half-modal
A half-modal is simple. You can complete it by just making a few small adjustments to the previous code.
.sheet(isPresented: $isModalView) ModalView() .presentationDetents([.medium])
Simply add the presentationDetents modifier and pass [.medium] as an argument to finish.
.medium is one of the elements of the PresentationDetent structure. PresentationDetent is a Hashable and Sendable structure that is defined as a standard in SwiftUI.
The reason why you need to pass it as an array even though it is just .medium is because there are cases where you might pass multiple elements here. For example, you can also pass [.medium, .large]. If you pass [.medium, .large], the user can drag to display the screen in full-screen mode in addition to the half-screen display. [.medium] does not allow for full-screen display.