SwiftUI dismiss modal

前端 未结 13 2183
闹比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-30 00:16

    Automatically pop if in Navigation or dismiss if Modal


    Just take the presentationMode from the environment in the destination view and dismiss the wrappedValue from it:

    struct DestinationView: View {
        @Environment(\.presentationMode) private var presentationMode
    
        var body: some View {
            Button("Dismiss") {
                self.presentationMode.wrappedValue.dismiss()
            }
        }
    }
    
    

    Demo ( pop / dismiss )

提交回复
热议问题