Search code examples
swiftiphonenavigationuinavigationcontroller

UINavigation Controller from a ViewController


Im trying to push a SecondViewController as a Navigation from a HomeViewController without a Navigation yet. However after I pushed SecondViewController the transition occurs but theres no navigation bar shown. What would be the best way to perform a transition as Navigation from a ViewController?

What I do into AppDelegate (im doing into appdelegate because im dealing with pushNotifications responses) is verify if the current Controller has a navigation:

extension UIApplication {
    
    class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        
        if let nav = base as? UINavigationController {
            return getTopViewController(base: nav.visibleViewController)
            
        } else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
            return getTopViewController(base: selected)
            
        } else if let presented = base?.presentedViewController {
            return getTopViewController(base: presented)
        }
        return base
    }
}

So if a Navigation is available I return it then I call another Function:

if let _topVC = UIApplication.getTopViewController(), UserHelper.shared.user.cpf != nil{
            _topVC.didAcceptPushNotification()
        }

So as the ViewController has a Navigation I created a Extension of ViewController to get the set the navigation foward:

extension UIViewController{
    @objc func didAcceptPushNotification() {
        DeepLinkManager.shared.checkDeepLink(navigationController: navigationController)
    }
}

Then I just say:

let notificationCenterVC = NotificationCentralListViewController(user: UserHelper.shared.user, notificationSpotlightId: nil)
        self.navigationController?.pushViewController(notificationCenterVC, sender: self)

But theres no navigation bar shown after the VC transition. What Im doing wrong?


Solution

  • Finally I figured out the error. Basically I called self.navigationController?.navigationBar.isHidden = false before pushing the SecondViewController, then the Navigation showed up:

    private func goToCentralDeNotificacoes() {
            let notificationCenterVC = NotificationCentralListViewController(user: UserHelper.shared.user, notificationSpotlightId: nil)
            self.navigationController?.navigationBar.isHidden = false
            self.navigationController?.pushViewController(notificationCenterVC, animated: true)
        }