SwiftUI. Navigation toolbar not working after dismissing modal

Hi,

Anyone encountered this?

I bring up a modal sheet using my add button. Then I dismiss it by swiping down. Immediately after that, the button is unresponsive and I cannot bring up the modal sheet again.

However, if I scroll the list, the button is functional again. I can include a video of what I see if you like.

Snippet of my code below.

struct TaskToDoView: View {

    @Environment(\.managedObjectContext) private var viewContext

    @FetchRequest(

        sortDescriptors: [NSSortDescriptor(keyPath: \SimpleRecord.title, ascending: true)],

        animation: .default)

    private var items: FetchedResults<SimpleRecord>



    @State private var isModal = false

    

    var body: some View {

        NavigationView {

            List {

                ForEach(items) { item in

                    NavigationLink(destination: ToDoModelView()) {

                        Text("Item at \(item.title!)")

                    }

                    

                }

                .onDelete(perform: deleteItems)

            }

            .toolbar {

                HStack {

                    #if os(iOS)

                    EditButton()

                    #endif 

                    Button(action: addItem) {

                        Label("Add Item", systemImage: "plus")

                    }

                    .sheet(isPresented: $isModal,

                            onDismiss: dismissToDoModelView,

                            content: {

                        ToDoModelView()

                    })

                }

            }

        }

    }

    private func dismissToDoModelView() {
        print("Modal Dismissed")
        print("Modal is \(self.isModal)")
    }

    private func addItem() {
        self.isModal.toggle()
    }
....

}

maybe it is to do with ToDoModelView(), because it works well in my simple test on ios 15 and macos 12.

Nope. I replaced it with Text("Hi"). Same problem.

I'm still using Xcode 12 and macOS 11.

This is the code I used for my test, on macOS 12, using Xcode 13 tested on ios15. Of course could be different on xcode 12. It also works if I put the sheet outside the toolbar.


import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
struct TaskToDoView: View {

    @Environment(\.managedObjectContext) private var viewContext

//    @FetchRequest(
//        sortDescriptors: [NSSortDescriptor(keyPath: \SimpleRecord.title, ascending: true)],
//        animation: .default)
//
//    private var items: FetchedResults<SimpleRecord>

    @State var items = ["one","two","three"]

    @State private var isModal = false

    var body: some View {
        NavigationView {
            List {
                ForEach(items, id: \.self) { item in  // <-- note the id:
                    NavigationLink(destination: Text("navlinkview: \(item)")) {
                        Text("Item at \(item)")
                    }
                }
               // .onDelete(perform: deleteItems)
            }
            .toolbar {
                HStack {
                    #if os(iOS)
                    EditButton()
                    #endif
                    Button(action: addItem) {
                        Label("Add Item", systemImage: "plus")
                    }
                    .sheet(isPresented: $isModal,
                            onDismiss: dismissToDoModelView,
                            content: {
                        Text("ToDoModelView")
                    })
                }
            }
        }
    }

    private func dismissToDoModelView() {
        print("Modal Dismissed")
        print("Modal is \(self.isModal)")
    }

    private func addItem() {
        self.isModal.toggle()
    }

}

struct ContentView: View {
    var body: some View {
        TaskToDoView()
    }
}

Sorry,

I get the same problem with macOS11 and Xcode 12 with your code. Sounds like something they just fixed in Xcode 13. I don't want to upgrade to macOS12 beta yet.

SwiftUI. Navigation toolbar not working after dismissing modal
 
 
Q