Created
May 16, 2025 17:59
-
-
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.
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 | |
| 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) { | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apply
.allowsSwipeBackWhenNavBarHidden()to the root view inside yourNavigationStack: