Search code examples
iosswiftuiviewcontrolleruikit

How to transition out of parent UINavigationController


I have the following code, which setups and shows a UIViewController to be embedded into a UINavigationController:

private func transitionToMainVC() {
  let vc = UINavigationController(rootViewController: SpacesVC())

  DispatchQueue.main.async {
    self.show(vc, sender: self)
  }
}

Now, inSpacesVC I want to show() another UIViewController, but outside of the "parent" UINavigationController.

When I use the following code in SpacesVC:

// Called by a button touch up inside
@objc private func transitionToMainVC() {
  let vc = NextVC()

  self.show(vc, sender: self)
}

It transitions to NextVC but it shows the navigation bar at the top of the screen; i.e. the new view controller is still embedded under the UINavigationController defines in the first snippet.

I am aware of the possibility of hiding the navigation bar in NextVC as such:

override func viewWillAppear(_ animated: Bool) {
  super.viewWillAppear(animated)
  navController.isNavigationBarHidden = true
}

But I want to show() the NextVC without embedding it in the navigation controller, since I won't need that anymore. How can I do that?


Solution

  • Use this method instead of show.

    func present(_ viewControllerToPresent: UIViewController, 
        animated flag: Bool, 
      completion: (() -> Void)? = nil)