Search code examples
iosswiftuinavigationcontroller

How To Present A NavigationController With the Second Controller


I want to present a UINavigationController modally however, I want to present the second controller within that stack rather than the root. How can this be done?


Solution

  • UINavigationController provides a method setViewControllers(_:animated:) and with its help you can configure the navigation stack the way you want.

    let navigationController = UINavigationController()
    navigationController.setViewControllers([rootViewController, secondViewController], animated: false)
    self.present(navigationController, animated: true)