1

How to dismiss fullScreenCover in SwiftUI

 1 year ago
source link: https://sarunw.com/posts/swiftui-dismiss-fullscreencover/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to dismiss fullScreenCover in SwiftUI

07 Nov 2022 ⋅ 3 min read ⋅ SwiftUI

Table of Contents

A modal presentation (fullScreenCover) is one of the core presentations in iOS. SwiftUI has many ways to dismiss a modal view based on how you structure your view and the minimum iOS version you support.

There are three ways to dismiss a modal in SwiftUI.

You can use the first two options regardless of the iOS version. For the @Environment option, we have two ways to do it based on the iOS version.

You can easily support sarunw.com by checking out this sponsor.

Build, manage, and grow in-app purchases:

Sponsor sarunw.com and reach thousands of iOS developers.

How to dismiss modal in the same view

If a modal view sits in the same view that presenting it, we can easily dismiss the modal view by controlling the same binding value (isPresented) since it has access to the binding.

struct ContentView: View {
@State private var isPresented = false

var body: some View {
Button("Detail") {
isPresented = true
}
.fullScreenCover(isPresented: $isPresented) {
// Detail View
Button("Back to Main") {
isPresented = false
}
}
}
}

The detail view Button("Back to Main") can reach isPresented. So, we can easily dismiss it by setting isPresented to false to dismiss the modal.

Set isPresented to false to dismiss a modal.

Set isPresented to false to dismiss a modal.

Most of the time, the view that you want to present might be too complicated to fit within a fullScreenCover closure. So, you probably separate it into another file.

Here we create a new DetailView that we want to present.

struct DetailView: View {
var body: some View {
Button("Back to Main") {

}
}
}

With a separate view like this, we no longer have access to the isPresented binding.

You have two ways to dismiss a modal view that isn't on the same view.

How to dismiss modal with @Binding

One way to dismiss a presented view is to pass the isPresented binding to that view.

In this example, we add an isPresented binding to the DetailView.

struct DetailView: View {
// 1
@Binding var isPresented: Bool

var body: some View {
Button("Back to Main") {
// 2
isPresented = false
}
}
}

1 The DetailView accept the isPresented binding value.
2 Then, we use it to dismiss the modal.

To use it, we pass an isPresented binding from the presenting view to a modal view.

struct ContentView: View {
// 1
@State private var isPresented = false

var body: some View {
Button("Detail") {
isPresented = true
}
.fullScreenCover(isPresented: $isPresented) {
// 2
DetailView(isPresented: $isPresented)
}
}
}

We pass down isPresented 1 from the presenting view to presented view, DetailView 2.

You can easily support sarunw.com by checking out this sponsor.

Build, manage, and grow in-app purchases:

Sponsor sarunw.com and reach thousands of iOS developers.

Dismiss a modal with @Environment

The other way for the view to dismiss itself is using environment value via the @Environment property wrapper.

Which environment value to use is depends on the iOS version you supported.

How to dismiss modal in iOS 13/14

For a view to dismissing itself, we have to add @Environment(\.presentationMode) var presentationMode to a presented view.

We can dismiss the view by calling presentationMode.wrappedValue.dismiss().

struct DetailView: View {
@Environment(\.presentationMode) var presentationMode

var body: some View {
Button("Back to Main") {
presentationMode.wrappedValue.dismiss()
}
}
}

Then we can use DetailView as a modal view without passing anything.

struct ContentView: View {
@State private var isPresented = false

var body: some View {
Button("Detail") {
isPresented = true
}
.fullScreenCover(isPresented: $isPresented) {
DetailView()
}
}
}

How to dismiss modal in iOS 15

In iOS 15, Apple deprecated the presentationMode environment value, replacing it with a dismiss action (dismiss).

For a view to dismissing itself, we have to add @Environment(\.dismiss) private var dismiss to a presented view.

We can dismiss the view by calling dismiss().

struct DetailView: View {
@Environment(\.dismiss) var dismiss

var body: some View {
Button("Back to Main") {
dismiss()
}
}
}

Then we can use DetailView as a modal view without passing anything.

struct ContentView: View {
@State private var isPresented = false

var body: some View {
Button("Detail") {
isPresented = true
}
.fullScreenCover(isPresented: $isPresented) {
DetailView()
}
}
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK