Created
April 25, 2025 04:57
-
-
Save ajjames/ad5258b9bd40486b7b7ba98b3b0cc7ed to your computer and use it in GitHub Desktop.
adds swipe-left-to-dismiss
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 | |
import UIKit | |
/// A ViewModifier that re-enables the default iOS edge-swipe back gesture in a SwiftUI NavigationStack. | |
struct SwipeBackModifier: ViewModifier { | |
func body(content: Content) -> some View { | |
content | |
.background(SwipeBackEnabler().frame(width: 0, height: 0).allowsHitTesting(false)) | |
} | |
} | |
extension View { | |
/// Re-enable the interactive pop (edge-swipe) gesture on NavigationStack. | |
func enableSwipeBack() -> some View { | |
modifier(SwipeBackModifier()) | |
} | |
} | |
// MARK: - UIKit Representable to hook into UINavigationController | |
private struct SwipeBackEnabler: UIViewControllerRepresentable { | |
func makeUIViewController(context: Context) -> UIViewController { | |
let vc = UIViewController() | |
DispatchQueue.main.async { | |
vc.navigationController?.interactivePopGestureRecognizer?.delegate = context.coordinator | |
vc.navigationController?.interactivePopGestureRecognizer?.isEnabled = true | |
} | |
return vc | |
} | |
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {} | |
func makeCoordinator() -> Coordinator { Coordinator() } | |
class Coordinator: NSObject, UIGestureRecognizerDelegate { | |
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { true } | |
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, | |
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { | |
true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment