SwiftUI dismiss modal

前端 未结 13 2194
闹比i
闹比i 2020-11-29 23:23

Since SwiftUI is declarative there is no dismiss methode. How can is add a dismiss/close button to the DetailView?



        
13条回答
  •  鱼传尺愫
    2020-11-29 23:59

    In Xcode Beta 5, another way to do this is to use @State in the view that launches the modal, and add a binding in the modal view to control visibility of the modal. This doesn't require you to reach into the @Environment presentationMode variable.

    struct MyView : View {
        @State var modalIsPresented = false
    
        var body: some View {
            Button(action: {self.modalIsPresented = true})  {
                Text("Launch modal view")
            }
            .sheet(isPresented: $modalIsPresented, content: {
                MyModalView(isPresented: self.$modalIsPresented)
            })
        }
    }
    
    
    struct MyModalView : View {
        @Binding var isPresented: Bool
    
        var body: some View {
            Button(action: {self.isPresented = false})  {
                Text("Close modal view")
            }
        }
    }
    

提交回复
热议问题