Home > Enterprise >  SwiftUI: How to display a full screen view from a sheet
SwiftUI: How to display a full screen view from a sheet

Time:01-29

Currently, I have a Navigation Link within a sheet. This creates a new page inside that sheet instead of taking up the full screen. Is there a way to dismiss the sheet and then present the view on the original screen?

Original view with a button pulling up a sheet:

Button {
    userTappedItem = item
} label: {
    ...
}
.sheet(item: $userTappedItem) {
} content: { item in
    NewView(item: item)
}

NavigationLink within NewView:

NavigationLink {
    ViewIWantAsFullScreen()
} label: {
    ...
}

CodePudding user response:

Yes, this is doable. The most important bit is the delay between dismissing the sheet and presenting the full screen cover -- without this delay, the full screen cover View just replaces the sheet's content.

struct ContentView  : View {
    @State private var sheetDisplayed = false
    @State private var fullScreenDisplayed = false
    
    var body: some View {
        VStack {
            Button("Show Sheet") {
                sheetDisplayed.toggle()
            }.sheet(isPresented: $sheetDisplayed) {
                SheetView(closeAndDisplayFullScreen: {
                    sheetDisplayed = false
                    DispatchQueue.main.asyncAfter(deadline: .now()   0.5) {
                        fullScreenDisplayed = true
                    }
                    
                })
            }
        }.fullScreenCover(isPresented: $fullScreenDisplayed) {
            FullScreenView()
        }
    }
}

struct SheetView : View {
    var closeAndDisplayFullScreen : () -> Void
    
    var body: some View {
        Button("Show full screen after dismiss") {
            closeAndDisplayFullScreen()
        }
    }
}

struct FullScreenView : View {
    var body: some View {
        Text("Full screen")
    }
}
  •  Tags:  
  • Related