Swipe-back Gesture for Custom Navigation in SwiftUI
1 min readDec 7, 2023
Using a custom navigation in SwiftUI is a must for most apps to opt in for that perfect design. For that you need to hide the default navigation bar in your View. Hence, you will notice that good old swipe-back animated gesture is not there anymore. It is frustrating for you, your users and once again you I know.
For Good there is a solution for that using UIKit.
Just copy/paste this code in your project, test that swipe-back gesture in your app and voila!
extension UINavigationController: UIGestureRecognizerDelegate {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}
I hope that my piece was informative and helpful to you.
Farewell, keep growing and learning. See you in the next one. ✌️
Make sure to follow me on Twitter to keep receiving the newest articles: