美文网首页
SwiftUI presentViewController 实现

SwiftUI presentViewController 实现

作者: fordG | 来源:发表于2020-12-15 12:51 被阅读0次
    import SwiftUI
    
    struct Example: View {
        @State private var isPresented = false
    
            var body: some View {
                Button("Present!") {
                    self.isPresented.toggle()
                }
                .fullScreenCover(isPresented: $isPresented, content: FullScreenModalView.init)
            }
    }
    
    #if DEBUG
    struct Example_Previews: PreviewProvider {
        static var previews: some View {
            Example()
        }
    }
    #endif
    
    
    struct FullScreenModalView: View {
        @Environment(\.presentationMode) var presentationMode
    
        var body: some View {
            VStack {
                Text("This is a modal view")
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(Color.red)
            .edgesIgnoringSafeArea(.all)
            .onTapGesture {
                presentationMode.wrappedValue.dismiss()
            }
        }
    }
    
    #if DEBUG
    struct FullScreenModalView_Previews: PreviewProvider {
        static var previews: some View {
            FullScreenModalView()
        }
    }
    #endif
    
    • 另外一种方式
    struct Example: View {
        @State var showingDetail = false
    
            var body: some View {
                Button(action: {
                    self.showingDetail.toggle()
                }) {
                    Text("Show Detail")
                }.sheet(isPresented: $showingDetail) {
                    FullScreenModalView()
                }
            }
    }
    

    相关文章

      网友评论

          本文标题:SwiftUI presentViewController 实现

          本文链接:https://www.haomeiwen.com/subject/mzfwgktx.html