UINavigationController侧滑
import UIKit
class HPNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
let navBar = UINavigationBar.appearance()
navBar.barTintColor = UIColor.white
navBar.tintColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7)
navBar.titleTextAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 17)]
// 创建全局手势
self.initGlobalPan()
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
if viewControllers.count > 0 {
viewController.hidesBottomBarWhenPushed = true
viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(navigationBack))
}
super.pushViewController(viewController, animated: true)
}
@objc private func navigationBack() {
popViewController(animated: true)
}
func initGlobalPan() -> Void {
// 创建Pan手势
let target = interactivePopGestureRecognizer?.delegate
let globalPan = UIPanGestureRecognizer(target: target, action: Selector(("handleNavigationTransition:")))
globalPan.delegate = self.delegate as! UIGestureRecognizerDelegate?
self.view.addGestureRecognizer(globalPan)
// 禁止系统的手势
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return self.childViewControllers.count != 1
}
}