SwiftUI presentViewController 实现 fullScreenCover

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 实现 fullScreenCover)