通过重写UINavigationController push方法 实现多次push
import UIKit
class LYNavigationVc: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
//设置返回边缘手势代理
self.interactivePopGestureRecognizer?.delegate = self
}
//重写push方法
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
//导航控制器属性 多个子控件
print(viewControllers.count)
//只有一个
super.pushViewController(viewController, animated: animated)
//调用super super之后 才会将子视图添加到导航控制器
print(viewControllers.count)
//两个或者多个
if viewControllers.count > 1 {
if viewControllers.count == 2 {
//获取第一个导航栏里 子视图控制器的标题
let title = viewControllers[0].title!
viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
}else{
//统一改为返回标题
viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
}
viewController.title = "当前显示的是第\(viewControllers.count)级控制器"
}
}
/* bug:
界面跳转:阴影
window设置为白色
tabbar设置背景图片
*/
@objc private func popAction() {
self.popViewController(animated: true)
}
extension LYNavigationVc : UIGestureRecognizerDelegate {
//是否处理点击的边缘手势
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
//根控制器 不处理边缘手势
if viewControllers.count == 1{
return false
}
return true
}
}
网友评论