I have been using the following extension throughout iOS 15 and iOS 16 seemingly with no issues. For context, I implemented this in my project to offer the ability to retain the swipe-back gesture for navigating backwards in a view hierarchy (child to parent). This is because hiding the toolbar or navbar in SwiftUI with a custom implementation of a back button (for UI styling purposes) causes the swipe-back gesture to be lost.
That's where this extension comes into play. Which I found here, thanks to Nick Bellucci's top rated answer. Hide navigation bar without losing swipe back gesture in SwiftUI
The concern now, is that when returning to the root view after swiping back from a child view, the root view freezes and breaks in iOS 17.0 on both device and simulator. Has anyone reproduced this issue or encountered it? To fully recreate the scenario, simply use .toolbar(.hidden)
on a child view, or .navigationBarBackButtonHidden()
instead.
It's also relevant to note that I am using dismiss()
to tap into the view popping. Snippet below... you simply have to place the custom back button in your child view to your liking to recreate this example.
extension UINavigationController: UIGestureRecognizerDelegate {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}
struct CustomBackButton: View {
@Environment(\.dismiss) var dismiss
var body: some View {
Button(action: {
dismiss()
}) {
// your pretty swiftui code here
}
}
}
Edit: it's possible I've got myself into a 'square-peg in a round hole' situation. It's likely the solution is to leverage the existing navigation bar and overwrite the back button design while coming up with a way to hide the navigation bar. The caveat here, is that it can pad your content in an unexpected way at the top edge, so there may need to be some customized padding or margin on content that conflicts with the navigation bar's padding when it is hidden from view.