SwiftUI dismiss modal

前端 未结 13 2132
闹比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:24

    Use Environment variable at PresentationMode. This GitHub link will maybe help you to solve the problem https://github.com/MannaICT13/Sheet-in-SwiftUI

    This is simple solution:

    struct ContentView2 : View {
    
        @Environment (\.presentationMode) var presentationMode
    
        var body : some View {
            VStack {
                Text("This is ContentView2")
                Button(action: {
                    self.presentationMode.wrappedValue.dismiss()
                }, label: {
                    Text("Back")    
                })    
            }
        }
    }
    
    
    struct ContentView: View {
    
        @State var isShowingSheet : Bool = false
    
        var body: some View {
            Button(action: {
                self.isShowingSheet.toggle()
            }, label: {
                Text("Click Here")
            }).sheet(isPresented: $isShowingSheet, content: {  
                ContentView2()
            })
        }
    }
    
    0 讨论(0)
提交回复
热议问题