UINavigationController 返回手势与 leftBarButtonItem
UINavigationController 返回手势与 leftBarButtonItem
UINavigationController 自带从屏幕左侧边缘向右滑动的返回手势,可以通过这个手势实现 pop,或者 pop 中途取消 pop 而停留在当前控制器(UIViewController)。如果设置了当前控制器的 navigationItem.leftBarButtonItem 或 navigationItem.leftBarButtonItems,则返回手势失效。可以通过自定义 NavigationController,实现设置 navigationItem.leftBarButtonItem 或 navigationItem.leftBarButtonItems,并且保留返回手势。代码已上传 GitHub:https://github.com/Silence-GitHub/NavigationControllerDemo
UINavigationController 的 interactivePopGestureRecognizer 属性为返回手势。设置手势的 delegate,手动管理返回手势。当 UINavigationController 没有或只有一个子控制器时,返回手势不能响应,否则会出现奇怪的现象。自定义 currentNotRootVC 属性,记录当前的非根子控制器(只有一个子控制器时,此子控制器为根子控制器,root view controller)。设置 UINavigationController 的 delegate,通过 UINavigationControllerDelegate 的方法,更新 currentNotRootVC。代码示例如下
import UIKit
class NavigationController: UINavigationController, UINavigationControllerDelegate, UIGestureRecognizerDelegate {
// Current child view controller, but not root view controller
private var currentNotRootVC: UIViewController?
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
interactivePopGestureRecognizer?.delegate = self
}
// MARK: - UINavigationControllerDelegate
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
currentNotRootVC = viewControllers.count <= 1 ? nil : viewController
/*
switch viewController {
case is SomeVCClassForbidPopGesture:
interactivePopGestureRecognizer?.isEnabled = false
default:
interactivePopGestureRecognizer?.isEnabled = true
}
*/
}
// MARK: - UIGestureRecognizerDelegate
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if interactivePopGestureRecognizer == gestureRecognizer {
// Begin to pop only when top view controller is current child view controller but not root view controller
return currentNotRootVC == topViewController
}
return true
}
}
如果有部分控制器不允许返回手势(例如编辑信息,退出页面需要确认),则在func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool)
方法中,加入对 viewController 的判断,开启或禁用返回手势。
参考:http://www.cnblogs.com/v2m_/p/3521569.html
转载请注明出处:http://www.cnblogs.com/silence-cnblogs/p/6618056.html