Skip to content

Instantly share code, notes, and snippets.

@philipshen
Last active March 10, 2025 14:29
Show Gist options
  • Select an option

  • Save philipshen/8bfc229d986ad0ac13cbc3ac890f3c8e to your computer and use it in GitHub Desktop.

Select an option

Save philipshen/8bfc229d986ad0ac13cbc3ac890f3c8e to your computer and use it in GitHub Desktop.
Fade in full screen cover in Swift
import SwiftUI
struct FadeInFullScreenCoverModifier<V: View>: ViewModifier {
@Binding var isPresented: Bool
@ViewBuilder let view: () -> V
@State var isPresentedInternal = false
func body(content: Content) -> some View {
content
.fullScreenCover(isPresented: Binding<Bool>(
get: { isPresented },
set: { isPresentedInternal = $0 }
)) {
Group {
if isPresentedInternal {
view()
.transition(.opacity)
.onDisappear { isPresented = false }
}
}
.onAppear { isPresentedInternal = true }
.presentationBackground(.clear)
}
.transaction {
// Disable default fullScreenCover animation
$0.disablesAnimations = true
// Add custom animation
$0.animation = .easeInOut
}
}
}
extension View {
func fadeInFullScreenCover<V: View>(
isPresented: Binding<Bool>,
content: @escaping () -> V
) -> some View {
modifier(FadeInFullScreenCoverModifier(
isPresented: isPresented,
view: content
))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment