ios - 以编程方式创建时不显示导航 Controller

标签 ios swift xcode uinavigationcontroller ios13

我刚刚更新到 iOS 13/Xcode 11,我正在尝试将 UIViewController 嵌入到 UINavigationController 中,就像我过去所做的那样。 ViewController 已显示,但我的 NavigationController 未显示。下面是我在 AppDelegate 内的 didFinishLaunchingWithOptions 方法中的内容。

    window = UIWindow()

    let vc = ViewController()
    let navController : UINavigationController = UINavigationController(rootViewController: vc)
    navController.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.black]

    window?.rootViewController = navController
    window?.makeKeyAndVisible()

编辑: 我发现我的错误,我试图以编程方式创建 VC,但我将 Storyboard作为“初始 View Controller ”。即使我清除了项目的常规设置中的“主界面”字段,这也完全忽略了我在 appdelegate 中的代码。

我还想指出,我的代码不应该位于 appdelegate 中,而应该位于 SceneDelegate 中。更多信息请参见:https://medium.com/@ZkHaider/apples-new-uiscene-api-a-programmatic-approach-52d05e382cf2

最佳答案

也许你可以尝试使用导航 Controller 的标识符

    let destination = storyboard.instantiateViewController(withIdentifier: "--navigation controller identifier--") as! --class name--
    navigationController?.pushViewController(destination, animated: true)

关于ios - 以编程方式创建时不显示导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350712/

相关文章:

ios - Xcode 工作区文件为空

ios - 更改图钉颜色 MKMapView

ios - 何时使用 respondsToSelector 与 objc_getClass

xcode - 在 Facebook 上快速分享

ios - 如何停止 UISegmentedControl 分段动画

ios - 在 iOS 应用程序中打包 6000 多个声音文件的最佳方法是什么

swift - 如何在 Swift 3 中将字典表示为 NSValue?

ios - 在 View 内创建导航 Controller

ios - NSLocalizedString 返回无处定义的文本

ios - 是否可以通过 xcode 更改 iOS 应用程序的名称?