Search code examples
iosswiftuinavigationcontrolleruinavigationbar

Navigation bar disappear when navigate from AppDelegate


I need to navigate to a specific view controller when user click on notification tray.Therefore I need to redirect them from AppDelegate class:

Here is the code in AppDelegate :

if let myVc = UIStoryboard(name : "Main",bundle : nil).instantiateViewController(withIdentifier: "MyViewController")
    as? MyViewController {

         myVc.postId = Int(postId)
         if let navigationController = self.window?.rootViewController /* as? UINavigationController */{
          navigationController.show(myVc, sender: Any?.self)
         }
  }

By using this code,when I click on the notification tray,I can redirect to my MyViewController,but the problem is the navigation bar is gone,so the app is just stuck over there.

I tried using this as well,but by the code below,it cant redirect to myPostVc:

if let navigationController = self.window?.rootViewController  as? UINavigationController {
  navigationController.pushViewController(myVc, animated: true)
}

So my question is:

How to navigate to a specific view controller from AppDelegate with Navigation bar appear on destination view controller?


Solution

  • Embed the tabBar inside a navigation Controller (in code as the embed is grayed when you select a tabbar and want to embed it inside a naigation controller in storyboard) and use this to push the VC

     if let navigationController = self.window?.rootViewController  as? UINavigationController {
        navigationController.pushViewController(myVc, animated: true)
     }
    

    OR you can do this

    if let cu = self.window?.rootViewController as? UITabBarController
     {         
           let nav: UINavigationController = UINavigationController()
    
            self.window?.rootViewController = nav
    
            let str = UIStoryboard.init(name: "Main", bundle: nil)
    
            let rr = str.instantiateViewController(withIdentifier: "idOFPushedVC") 
    
             nav.setViewControllers([cu,rr], animated: true)
    
      }