如何开启:
1.ViewController实现 UIGestureRecognizerDelegate 协议
class YourViewController: UIGestureRecognizerDelegate { func viewDidLoad() { navigationItem.setLeftBarButtonItems([...,...]) // swipe back 失效了 self.navigationController?.interactivePopGestureRecognizer.delegate = self // 启用 swipe back } }
若想禁用Swipe back,可以在viewWillAppear 或者 viewDidAppear里关闭它:
override func viewDidAppear(animated: Bool) { navigationController?.interactivePopGestureRecognizer.enabled = false // disable pop gesture }
需要注意的是,启用swipe back对当前NavigationController管理的所有viewcontrollers有效,不需要所有的ViewController都调用那个方法,只要保证在同一个UINavigationController里即可。
时间: 2024-11-03 05:28:21