如何过渡出父级 UINavigationController

How to transition out of parent UINavigationController

我有以下代码,它设置并显示要嵌入到 UINavigationController 中的 UIViewController:

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

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

现在,在SpacesVC我想show()另一个UIViewController,但是在"parent"UINavigationController.

之外

当我在SpacesVC中使用以下代码时:

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

  self.show(vc, sender: self)
}

它过渡到 NextVC 但它在屏幕顶部显示导航栏;即新的视图控制器仍然嵌入在第一个片段中的 UINavigationController 定义下。

我知道在 NextVC 中隐藏导航栏的可能性:

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

但我想 show() NextVC 而不是将其嵌入导航控制器,因为我不再需要它了。我该怎么做?

使用 this method 代替 show

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