Skip to content

Instantly share code, notes, and snippets.

@aaronlab
Created March 13, 2021 02:53
Show Gist options
  • Save aaronlab/b2600c0828ff923d8e15cd8403575a91 to your computer and use it in GitHub Desktop.
Save aaronlab/b2600c0828ff923d8e15cd8403575a91 to your computer and use it in GitHub Desktop.
A view modifier to use '.sheet ' and '.fullScreenCover' at the same time
struct ChooseableSheet<Destination>: ViewModifier where Destination: View {
@Binding var isFullScreen: Bool
@Binding var isPresented: Bool
private let fullScreenContent: () -> Destination?
private let modalContent: () -> Destination?
init(
isFullScreen: Binding<Bool>,
isPresented: Binding<Bool>,
@ViewBuilder fullScreenContent: @escaping () -> Destination? = { nil },
@ViewBuilder modalContent: @escaping () -> Destination? = { nil }
) {
self._isFullScreen = isFullScreen
self._isPresented = isPresented
self.fullScreenContent = fullScreenContent
self.modalContent = modalContent
}
func body(content: Content) -> some View {
if isFullScreen {
return AnyView(
content
.fullScreenCover(isPresented: $isPresented) {
fullScreenContent()
}
)
} else {
return AnyView(
content
.sheet(isPresented: $isPresented) {
modalContent()
}
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment