article

Friday, June 11, 2021

SwiftUI full screen modal using fullScreenCover

SwiftUI full screen modal using fullScreenCover

SwiftUI’s fullScreenCover() modifier gives us a presentation style for times when you want to cover as much of the screen as possible, and in code it works almost identically to regular sheets.

ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
//  ContentView.swift
//  Test
//
//  Created by Cairocoders
//
 
import SwiftUI
 
struct ContentView: View {
    @State private var isPresented = false
 
    var body: some View {
        Button("Present!") {
            isPresented.toggle()
        }
        .fullScreenCover(isPresented: $isPresented, content: FullScreenModalView.init)
    }
}
 
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
 
struct FullScreenModalView: View {
 
    @Environment(\.presentationMode) var presentationMode
     
    var body: some View {
        Text("modal view")
         
        Button("Dismiss Modal") {
            presentationMode.wrappedValue.dismiss()
        }
    }
}

Related Post