使用导航栏以模态方式呈现 ViewController 不更改 stackView

Present ViewController modally with navigation Bar not changing the stackView

我正在尝试以模态方式呈现一个视图控制器,我希望它有一个导航栏。

我在这里发现了一个类似的问题presenting ViewController with NavigationViewController swift

我不想更改 RootViewController。我想在 VC 中添加 navigationBar 或已经用一个模态呈现 navigationBar。

谢谢

您始终可以创建一个 UINavigationController 实例并将您的视图控制器(您想要以模态方式呈现的那个)添加到其中。然后,模态呈现新的 UINavigationController 实例而不是您的视图控制器。这将显示您的视图控制器(因为它包含在 UINavigationController 中)和导航栏(因为它在 UINavigationController 中)。

这是一个例子:

let rootViewController = UITabBarController() // Lets assume this is your root view controller
let modalViewController = UIViewController() // Lets assume this is the view controller you want to present modally

// Here we wrap the modal controller in a navigation controller and then present it modally
let navigationController = UINavigationController(rootViewController: modalViewController)
rootViewController.present(navigationController, animated: true, completion: nil)