使用导航栏以模式显示ViewController,而不更改stackView



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

我在这里发现了一个类似的问题,用NavigationViewController swift 演示ViewController

我不想更改RootViewController。我想在VC中添加导航栏,或者在模式上已经添加了导航栏。

感谢

您可以始终创建一个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)

最新更新