SwiftUI - Navigation bar button not clickable after sheet has been presented

前端 未结 5 816
攒了一身酷
攒了一身酷 2020-12-14 17:39

I have just started using SwiftUI a couple of weeks ago and i\'m learning. Today I ran into a into an issue.

When I present a sheet with a navigationBarItems-button

5条回答
  •  误落风尘
    2020-12-14 18:22

    I think this happens because the presentationMode is not inherited from the presenter view, so the presenter didn't know that the modal is already closed. You can fix this by adding presentationMode to presenter, in this case to ContentView.

    struct ContentView: View {
    
        @Environment(\.presentationMode) var presentation
        @State var showSheet = false
    
        var body: some View {
            NavigationView {
                VStack {
                    Text("Test")
                }.sheet(isPresented: self.$showSheet) {
                    ModalView()
                }.navigationBarItems(trailing:
                    Button(action: {
                        self.showSheet = true
                    }) {
                        Text("SecondView")
                    }
                )
            }
        }
    }
    

    Tested on Xcode 11.5.

    Here is the full working example.

提交回复
热议问题