Last active
March 10, 2025 14:29
-
-
Save philipshen/8bfc229d986ad0ac13cbc3ac890f3c8e to your computer and use it in GitHub Desktop.
Fade in full screen cover in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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