Swipe back gesture not working in iOS 17 when NavBar is hidden - Swift UI

1.6k Views Asked by At

I have been using the following extension in Swift UI to enable swipe back gestures even when the navigation bar is hidden. However, after updating to iOS 17 (beta), it seems that this code is no longer working. I'm not sure if this is a bug in the new iOS version or if there's a different approach to achieve this functionality. Can anyone help me troubleshoot this issue or suggest an alternative solution?

extension UINavigationController: UIGestureRecognizerDelegate {
   open override func viewDidLoad() {
    super.viewDidLoad()
    interactivePopGestureRecognizer?.delegate = self
  }

  // Allows swipe back gesture after hiding standard navigation bar with .navigationBarHidden(true).
  public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    viewControllers.count > 1
  }

  // Allows interactivePopGestureRecognizer to work simultaneously with other gestures.
  public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    viewControllers.count > 1
  }

  // Blocks other gestures when interactivePopGestureRecognizer begins (my TabView scrolled together with screen swiping back)
  public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    viewControllers.count > 1
  }

}

Steps to Reproduce:

Create a SwiftUI project. Use the provided extension to enable swipe back gestures. Set .navigationBarHidden(true) on a view. Try to swipe back on the view. Expected Behavior: The view should respond to the swipe back gesture, even with the navigation bar hidden.

Actual Behavior: The swipe back gesture doesn't work, and the view remains unchanged.

Additional Information:

I've tested this code on iOS 16, and it works as expected. I've verified that the issue occurs specifically on iOS 17 beta. My Xcode version is [15A5219j]. I haven't made any other significant changes to my code. Any assistance in resolving this issue or suggestions for alternative approaches would be greatly appreciated. Thank you!

By providing this information, you should receive relevant assistance from the Stack Overflow community, whether it's a solution to the problem or guidance on any changes needed for compatibility with iOS 17.

1

There are 1 best solutions below

0
Eilgnaw On

may be you can try this

import UIKit
    
extension UINavigationController {
    override open func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        navigationBar.isHidden = AppState.shared.navHideen
    }
}

class AppState {
    static let shared = AppState()
    var navHideen = false
}

TestView()
   .onAppear {
        AppState.shared.navHideen = true
   }
   .onDisappear {
       AppState.shared.navHideen = false
   }