Search code examples
iosswiftuinavigationcontrolleruitabbarcontrollerhierarchy

How can I embed NavigationController to Viewcontroller in xib


I have a viewcontroller lets say A that created with xib and I want to add navigationController to it .

View hierarchy :

UITabbarController -> NavigationController -> B Viewcontroller (A added tabbarcontroller in here when a button in B click)

I add A view to Tabbar in B Viewcontroller's button action like

tabBarController?.view.addSubview(A.view)

With this result , View A appears on Tabbar that what I want.

In A viewController there is a Tableview has some datas.When I click a row I want to push another Viewcontroller with self.navigationController.push(..) But I can't push anything because UITabbarController doesnt have a navigationController so I want to add NavigationController to A viewController . I searched lots of things but can't find any possible solution for it.

How can I achieve this?


Solution

  • Set the rootViewController of UINavigationController and add it to the view hierarchy.

    let navigationController = UINavigationController(rootViewController: aViewController)
    

    Also, to append a new view controller to the UITabBarController's viewControllers use:

    tabBarController?.viewControllers?.append(navigationController)
    

    Or:

    tabBarController?.viewControllers?.insert(navigationController, at: 0)