Home > Back-end >  SwiftUI: @Environment(\.presentationMode)'s dismiss not working in iOS14
SwiftUI: @Environment(\.presentationMode)'s dismiss not working in iOS14

Time:07-07

I have a view that shows a sheet for filtering the items in a list. The view has this var:

var filter: some View {
        Button {
            self.showFilter = true
        } label: {
            Image(systemName: "line.horizontal.3.decrease.circle")
                .renderingMode(.original)
        }
        .sheet(isPresented: $showFilter) {
            FilterView($jobFilter, categoriesViewModel, jobsViewModel)
        }
    }

However, in the sheet, I'm trying the following and I can't make the view dismissed when clicking on the DONE button, only on the CANCEL button:

...
.toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button("FilterView.Button.Cancel.Text".capitalizedLocalization) {
                        presentationMode.wrappedValue.dismiss()
                    }
                }
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("FilterView.Button.Done.Text".capitalizedLocalization) {
                        let request = Job.defaultRequest()
                        
                        request.predicate = filterViewModel.buildPredicate(withJobFilterDraft: self.draft)
                        request.sortDescriptors = [NSSortDescriptor(key: #keyPath(Job.publicationDate), ascending: false)]
                        
                        jobsViewModel.filteredJobsFetchRequest = request
                        self.jobFilter = self.draft
                        presentationMode.wrappedValue.dismiss()
                    }
                }
            }

I have also tried with a @Binding like Paul says here but there's no luck. Is there any workaround, or am I doing something wrong?

Thanks in advance!

CodePudding user response:

In the sheet try adding this instead.

@Environment(\.dimiss) var dismiss 

Button("Some text") {
    // Code  
    dismiss() 
} 

CodePudding user response:

According to your code, I assumed your FilterView() is not a sub view, but an independent view by its own. Therefore, to make sure "presentationMode.wrappedValue.dismiss()" works, you don't need to create @Binding or @State variables outside the FilerView() for passing the data back and forth between different views. Just create one variable inside your FilterView() to make it works. I don't have your full code, but I created a similar situation to your problem as below code:

import SwiftUI

struct Main: View {
@State private var showFilter = false
var body: some View {
    Button {
        self.showFilter = true
    } label: {
        Image(systemName: "line.horizontal.3.decrease.circle")
            .renderingMode(.original)
    }
    .sheet(isPresented: $showFilter) {
       FilterView()
    }
}
}

struct FilterView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var body: some View {
    NavigationView {
        VStack {
            Text("Filter View")
        }.toolbar {
            ToolbarItem(placement: .navigationBarLeading) {
                Button {
                    presentationMode.wrappedValue.dismiss()
                } label: {
                    Text("cancel")
                }
            }
            ToolbarItem(placement: .navigationBarTrailing) {
                Button {
                    presentationMode.wrappedValue.dismiss()
                } label: {
                    Text("okay")
                }
            }
        }
    }
}
}

CodePudding user response:

the parameter onDismiss is missing:

.sheet(isPresented: $showFilter, onDismiss: { isPresented = false }) { ... }
  • Related