Skip to content

Instantly share code, notes, and snippets.

@ajjames
Created April 27, 2020 23:38
Show Gist options
  • Save ajjames/6c1b7cdb3719f6ed7e27c857ba17659e to your computer and use it in GitHub Desktop.
Save ajjames/6c1b7cdb3719f6ed7e27c857ba17659e to your computer and use it in GitHub Desktop.
SwiftUI fullscreen overlay modifier
import SwiftUI
struct OverlayView<OverlayContent: View>: ViewModifier {
@Binding var isPresented: Bool
var modalContent: OverlayContent
var transition: AnyTransition = .move(edge: .bottom)
var animation: Animation = .easeInOut
func body(content: Content) -> some View {
GeometryReader { geo in
ZStack {
content
VStack {
if self.isPresented {
self.modalContent
.transition(self.transition)
.animation(self.animation)
} else {
Spacer()
}
}
}
}
}
}
extension View {
func overlayModal<ModalContent: View>(isPresented: Binding<Bool>, @ViewBuilder modalContent: @escaping () -> ModalContent) -> some View {
self.modifier(OverlayView(isPresented: isPresented, modalContent: modalContent()))
}
}
#if DEBUG
struct OverFullContext_Previews: PreviewProvider {
static var isPresented: Bool = false
static var previews: some View {
RootView()
.edgesIgnoringSafeArea(.all)
}
}
#endif
struct RootView: View {
@State var isModalPresented: Bool = false
var body: some View {
ZStack {
Rectangle()
.fill(Color.blue)
Button(action: {
withAnimation {
self.isModalPresented.toggle()
}
}) {
Text("Show")
}
.foregroundColor(Color.white)
}
.overlayModal(isPresented: $isModalPresented) {
ModalView(isPresented: self.$isModalPresented)
}
}
}
struct ModalView: View {
@Binding var isPresented: Bool
var body: some View {
ZStack {
Rectangle()
.fill(Color.red)
Button(action: {
withAnimation {
self.isPresented.toggle()
}
}) {
Text("Dismiss")
}
.foregroundColor(Color.white)
} }
}
@joeldrotleff
Copy link

Is the GeometryReader necessary? It doesn't seem to be used, and removing it doesn't seem to break anything in iOS 16.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment