iOS中导航控制器的使用 UINavigationController

使用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

    }

}

 

转载于:https://www.cnblogs.com/xiaotian331/p/6659102.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值