Skip to content

Instantly share code, notes, and snippets.

@Chronos2500
Created May 16, 2025 17:59
Show Gist options
  • Select an option

  • Save Chronos2500/0a2b653fe0a1150c2023adc60797dc12 to your computer and use it in GitHub Desktop.

Select an option

Save Chronos2500/0a2b653fe0a1150c2023adc60797dc12 to your computer and use it in GitHub Desktop.
This should be declared only once in the root view of the NavigationStack.
import SwiftUI
extension View {
/// Allows swipe back when navigation bar is hidden.
/// This modifier should be applied to a view that resides within a NavigationStack.
/// - Returns: A view that allows swipe back when navigation bar is hidden.
public func allowsSwipeBackWhenNavBarHidden() -> some View {
modifier( AllowsSwipeBackWhenNavBarHiddenModifier() )
}
}
fileprivate struct AllowsSwipeBackWhenNavBarHiddenModifier: ViewModifier {
func body(content: Content) -> some View {
content
.background{ NavigationControllerWrapper() }
}
}
fileprivate struct NavigationControllerWrapper: UIViewControllerRepresentable {
@MainActor
final class ViewController: UIViewController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .clear
view.isUserInteractionEnabled = false
}
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
if let navigationController = parent?.navigationController as? UINavigationController {
navigationController.interactivePopGestureRecognizer?.delegate = self
navigationController.interactivePopGestureRecognizer?.isEnabled = true
}
}
}
func makeUIViewController(context: Context) -> ViewController {
.init()
}
func updateUIViewController(_ uiViewController: ViewController, context: Context) {
}
}
@Chronos2500
Copy link
Author

Apply .allowsSwipeBackWhenNavBarHidden() to the root view inside your NavigationStack:

struct ContentView: View {
    var body: some View {
        NavigationStack {
           NavigationLink~~
            .allowsSwipeBackWhenNavBarHidden()
        }
    }
}

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