美文网首页搜集知识iOS技巧学习_JMJC专题
iOS跳转到含有不同UINavigationItem的ViewC

iOS跳转到含有不同UINavigationItem的ViewC

作者: 阳光下慵懒的驴 | 来源:发表于2016-05-19 19:40 被阅读839次

    想实现这样跳转方法,原本就知道两个UINavigationController不能相互跳转,并且也懒得做自定义modal跳转动画,于是研究了三个小时总算有了两种实现方法

    简书APP PUSH跳转 简书APP PUSH跳转

    第一种:在viewWillAppear中设置nav bar的隐藏与显示

    1. 在AppDelegate中设置rootVC为含有nav的ViewController
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window?.makeKeyAndVisible()
    let vc = ViewController()
    self.window?.rootViewController = UINavigationController(rootViewController: vc)
    
    1. ViewController:
      在viewWillAppear中设置nav bar的隐藏
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 动画必须设置为true,否则会导致转场动画是nav bar不显示
        self.navigationController?.setNavigationBarHidden(true, animated: true) 
    }
    

    随意添加一个按钮实现跳转

    屏幕快照 2016-05-19 下午7.01.08.png
    1. 在SecondViewController中设置显示nav bar
      同样在viewWillAppear设置nav bar为显示
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        
        self.navigationItem.title = "首页"
       
        // 最好不要设置leftBarButton,会导致pop手势失效
        // self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "下一页", style: .Done, target: self, action: #selector(self.returnAction))
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    

    这样就实现了从无item到有item的跳转:

    1.gif
    1. 在ViewController中添加一个自定义的UINavigationBar
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.whiteColor()
            // 一定要设置nav的view的颜色,否则在跳转时自定义的bar右上角会有黑色。但是如果将自定义的bar设置为其他View(比如UIView)就不会有这种现象
            self.navigationController?.view.backgroundColor = UIColor.whiteColor()
            
            let customBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.width, 64))
            self.view.addSubview(customBar)
            
            let customItem = UINavigationItem(title: "首页")
            customItem.leftBarButtonItem = UIBarButtonItem(title: "进入", style: .Done, target: self, action: #selector(self.next))
            customBar.items = [customItem]
    }
    

    效果:


    2.gif

    第二种:封装基类,直接隐藏掉nav bar,添加一个自定义的bar,继承基类即会实现该效果,方便使用

    BaseViewController:

    class BaseViewController: UIViewController {
        
        // 使用自定义的navigationItem
        // 不能使用原本的navigationItem,当pop回来后里面的item会消失,可能和生命周期有关,不知道它都干了些什么
        var customNavigationItem = UINavigationItem(title: "")
        
        // 怎么私有化navigationItem啊!!这样会失败
    //    override private var navigationItem: UINavigationItem
    //    override private var navigationItem: UINavigationItem
    //    override private var navigationItem: UINavigationItem
    //    override private var navigationItem: UINavigationItem {
    //        get{
    //            return self.navigationItem
    //        }
    //    }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.whiteColor()
            // 隐藏原本的nav
            self.navigationController?.view.backgroundColor = UIColor.whiteColor() // 需要设置点颜色,否则push时右上角会显示一段黑色
            // 必须设置这两个属性,否则会导致pop手势失效
            self.navigationController?.navigationBar.shadowImage = UIImage()
            self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
            self.navigationController?.navigationBar.hidden = true
    
            // 不能使用这个方法,会导致pop手势失效,也不能把bar removeFromSuperView
    //        self.navigationController?.setNavigationBarHidden(true, animated: true)
            
            // 自定义的nav bar,直接加到view上
            let customBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.width, 64))
            self.view.addSubview(customBar)
            
            // 设置item
            customBar.items = [customNavigationItem]
        }
    }
    

    使用:
    ViewController:

    class ViewController: BaseViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            
            self.customNavigationItem.title = "首页"
            self.customNavigationItem.leftBarButtonItem = UIBarButtonItem(title: "下一页", style: .Done, target: self, action: #selector(self.next))
        }
        
        func next() {
            let sec = SecondViewController()
            self.navigationController?.pushViewController(sec, animated: true)
        }
    }
    

    SecondViewCotroller:

    class SecondViewController: BaseViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            self.view.backgroundColor = UIColor.whiteColor()
            
            self.customNavigationItem.title = "第二页"
            self.customNavigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .Done, target: self, action: #selector(self.returnAction))
        }
        
        func returnAction() {
            self.navigationController?.popViewControllerAnimated(true)
        }
    }
    

    效果:


    3.gif

    相关文章

      网友评论

        本文标题:iOS跳转到含有不同UINavigationItem的ViewC

        本文链接:https://www.haomeiwen.com/subject/sjygrttx.html