假设我有3个视图控制器VC1、VC2和VC3。第一个视图控制器是VC1,从这里我将转到VC2,从VC2到VC3。问题是我如何从VC3 -> VC1 not VC3 -> VC2 -> VC1回来,同时使用回推。
注意:
这是关于向后滑动的问题,而不是手动单击Back Button。
这是我的密码:
class MyNavigationController: UINavigationController {
override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
self.delegate = self
interactivePopGestureRecognizer?.delegate = self
navigationBar.isTranslucent = false
view.backgroundColor = .white
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
// do something
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
super.pushViewController(viewController, animated: animated)
interactivePopGestureRecognizer?.isEnabled = false
}
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
interactivePopGestureRecognizer?.isEnabled = true
}
}
extension PKNavigationController: UINavigationControllerDelegate, UIGestureRecognizerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
// Do Something
}
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}发布于 2022-09-12 11:17:28
在VC3中,每当您想要返回时,请使用:
navigationController?.popToRootViewController(animated: true)发布于 2022-09-12 12:40:56
现在还不完全清楚你想从你的问题中得到什么,但如果你的意思是想让后退按钮和滑动手势从VC3返回到vc1,就好像VC2不在导航堆栈中一样:在VC2按下VC3之后,使用UINavigationController's setViewControllers:animated: to [vc1, vc3],动画为false。那么导航堆栈和后退按钮上只有两个vc,然后滑动手势都会返回到VC1。
当然,对于用户来说,这可能是一种奇怪的体验,因为这不是导航堆栈的工作方式( vc2在哪里消失了?),所以您最好使用VC1推出某种容器VC,即在VC2和VC3之间过渡而不按下。
https://stackoverflow.com/questions/73688029
复制相似问题