A lightweight SwiftUI library that brings UIKit's interactive pop gesture control to SwiftUI navigation. Easily disable the swipe-back gesture on specific views in your navigation stack.
SwiftUI's NavigationStack and NavigationView don't provide a native way to disable the interactive pop gesture (swipe-back) on specific views. This can be problematic when you have:
- Forms with unsaved changes
- Multi-step processes that shouldn't be interrupted
- Custom navigation flows
- Views that need to prevent accidental back navigation
PopGestureRecognizerSwiftUI bridges the gap by accessing the underlying UINavigationController and controlling its interactivePopGestureRecognizer property through a simple SwiftUI view modifier.
- Simple API: Just add
.swipeBackGestureDisabled()to any view - Consecutive Support: Works with multiple consecutive disabled views
- Smart State Management: Automatically re-enables gesture when navigating back to enabled views
- iOS 16+ Compatible: Built for modern SwiftUI apps
- Lightweight: Minimal overhead and dependencies
Add PopGestureRecognizerSwiftUI to your project through Xcode:
- Go to File → Add Package Dependencies
- Enter the repository URL:
https://github.com/hayek/PopGestureRecognizerSwiftUI - Select the version and add to your target
Or add it to your Package.swift:
dependencies: [
.package(url: "https://github.com/hayek/PopGestureRecognizerSwiftUI", from: "1.0.0")
]Import the library and add the modifier to any view where you want to disable the swipe-back gesture:
import SwiftUI
import PopGestureRecognizerSwiftUI
struct ContentView: View {
var body: some View {
NavigationStack {
VStack {
NavigationLink("Go to Protected View", destination: ProtectedView())
}
.navigationTitle("Home")
}
}
}
struct ProtectedView: View {
var body: some View {
Text("Swipe back is disabled on this view!")
.navigationTitle("Protected")
.swipeBackGestureDisabled() // 🎯 This disables swipe back
}
}The library works by:
- Finding the UINavigationController: Uses
UIKitNavigationWrapperto locate the underlying navigation controller - State Tracking: Maintains navigation state for each view controller using
NavigationStateStore - Gesture Control: Disables/enables
interactivePopGestureRecognizerbased on view lifecycle - Smart Cleanup: Automatically cleans up state when views are deallocated
- iOS 16.0+
- Swift 6.0+
- Xcode 16.0+
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by the need to bring UIKit's navigation control to SwiftUI
- Thanks to the SwiftUI community for identifying this common pain point
If you have any questions or need help integrating this library, please:
- Open an issue on GitHub
- Check existing issues for similar problems
- Provide detailed information about your use case
Made with ❤️ for the SwiftUI community
