Search code examples
iosswiftuiviewcontrolleruinavigationcontrolleraddsubview

UIViewController containing a flow of UIViewControllers


I am building a UIViewController (outer viewController) that contains another UIViewController (inner viewController). To do this I am using a container view. Now I want the inner viewController to push to another UIViewController, so I basically want the inner viewController to be a child of a UINavigationController. I know that you cannot change the content of a container view once it has been initialised (not directly anyways), so I've hit a wall.

Does anyone have any thoughts on how I can nest a UINavigationController inside a UIViewController or do I need to rethink my approach to the problem?


Solution

  • Heavily inspired by @7vikram7 I ended up with a container view that embedded a UINavigationController which had my InnerViewController as its root viewController: Screen dump of the basic storyboard setup

    In my InnerViewController I am now able to push to any UIViewController, and control my navigation stack, for example:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Hide the navigation bar
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    
        // Push to whatever viewController I want to
        let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("myAwesomeViewController") as! AwesomeViewController
        self.navigationController?.pushViewController(viewController, animated: true)
    }