使用iOS的导航控制器,主要是实现UINavigationController的委托方法 UINavigationControllerDelegate。以下代码让MyNavigationController自己实现委托方法。
import UIKit class MyNavigationController: UINavigationController,UINavigationControllerDelegate { //由navigationController管理着的那些viewController在即将显示时,调用该函数 func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool){ switch viewController { case is ViewController1: print("第1个ViewController1将显示") case is ViewController2: print("第2个ViewController2将显示") default: print("未知ViewController") } print("当前导航栈顶是\(navigationController.topViewController!)") } //由navigationController管理着的那些viewController在显示完成后,调用该函数 func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool){ switch viewController { case is ViewController1: print("第1个ViewController1已显示") case is ViewController2: print("第2个ViewController2已显示") default: print("未知ViewController") } print("当前导航栈顶是\(navigationController.topViewController!)") } //这个navigationController委托实例的方法设置navigationController具有的重力感应旋转功能 func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask{ //all表示iPhone上这个navigationController具有三个方向的重力感应旋转功能,iPad具有所有4个方向上的重力感应旋转功能 return UIInterfaceOrientationMask.all //allButUpsideDown表示只有颠倒竖直方向不具有旋转功能。在iPhone上,allButUpsideDown与all相同 return UIInterfaceOrientationMask.allButUpsideDown //landscape表示手机分别向左右两边倒放,屏幕能跟着旋转,但是当手机重新竖立时,屏幕将无法旋转回来 return UIInterfaceOrientationMask.landscape //landscapeLeft表示手机向左边倒放,屏幕能跟着旋转,但是当手机重新竖立时,屏幕将无法旋转回 return UIInterfaceOrientationMask.landscapeLeft //同上 return UIInterfaceOrientationMask.landscapeRight //portrait表示这个navigationController不会跟着屏幕的转动旋转 return UIInterfaceOrientationMask.portrait //如果你在Info.plist上只配置了三个方向上的旋转功能,那么这里返回 portraitUpsideDown将会导致程序抛出异常 return UIInterfaceOrientationMask.portraitUpsideDown } //用来设定偏好方向 func navigationControllerPreferredInterfaceOrientationForPresentation(_ navigationController: UINavigationController) -> UIInterfaceOrientation{ //没在测试过,个人猜测是,类似于优酷这类视频app的全屏播放按钮,当按下全屏播放按钮时,app会选择其中一个landscape方向进行播放 return UIInterfaceOrientation.landscapeLeft } //用来设定切换动画 func navigationController(_ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?{ return nil } //这个方法帮助针对特定ViewController跳到另一ViewController时,作出相应操作 func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?{ switch fromVC{ case is ViewController1 where toVC is ViewController2 : print("从 ViewController1 到 ViewController2 ") case is ViewController2 where toVC is ViewController1 : print("从 ViewController2 到 ViewController1 ") default: print("未知跳转") } return nil } override func viewDidLoad() { super.viewDidLoad() self.delegate = self } }