如何開啓:
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裏即可。