In SwiftUI, what is the main difference in how a Sheet and a fullScreenCover are dismissed by default?
Think about the default user interaction for modal views in iOS.
Sheets in SwiftUI support swipe-to-dismiss by default, allowing users to drag down to close. fullScreenCover modals cover the entire screen and do not support swipe-to-dismiss by default; they require programmatic dismissal.
Which SwiftUI state variable type is best suited to control the presentation of a Sheet or fullScreenCover when you want to present a modal based on a Boolean condition?
Think about what type controls visibility: true means visible, false means hidden.
Using a Boolean @State variable is the standard way to control modal presentation in SwiftUI. Setting it to true shows the modal, false hides it.
When a fullScreenCover is dismissed in SwiftUI, what happens to the parent view's lifecycle?
Consider how modal presentation affects the underlying view in SwiftUI.
Dismissing a fullScreenCover returns control to the parent view without recreating or resetting it. The parent view remains active with its current state intact.
Which SwiftUI code snippet correctly presents a Sheet that passes a binding to a detail view?
struct ContentView: View {
@State private var showSheet = false
@State private var selectedName = ""
var body: some View {
Button("Show Sheet") {
showSheet = true
}
.sheet(isPresented: $showSheet) {
DetailView(name: $selectedName)
}
}
}
struct DetailView: View {
@Binding var name: String
var body: some View {
TextField("Enter name", text: $name)
}
}Remember to pass bindings with a $ prefix and use the correct parameter name.
The sheet modifier requires a binding to a Boolean for isPresented. The detail view expects a binding, so pass $selectedName. Option C correctly uses $showSheet and $selectedName.
Given this SwiftUI code, why does the fullScreenCover not dismiss when the user swipes down?
struct ContentView: View {
@State private var showModal = true
var body: some View {
Button("Show") { showModal = true }
.fullScreenCover(isPresented: $showModal) {
ModalView()
}
}
}
struct ModalView: View {
var body: some View {
Text("Modal")
}
}Think about default user interactions for fullScreenCover modals.
fullScreenCover modals cover the entire screen and do not support swipe-to-dismiss by default. You must dismiss them by changing the binding variable to false programmatically.