Skip to content

Instantly share code, notes, and snippets.

@ajjames
Created April 25, 2025 04:57
Show Gist options
  • Save ajjames/ad5258b9bd40486b7b7ba98b3b0cc7ed to your computer and use it in GitHub Desktop.
Save ajjames/ad5258b9bd40486b7b7ba98b3b0cc7ed to your computer and use it in GitHub Desktop.
adds swipe-left-to-dismiss
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