我以编程方式创建的导航控制器未出现在示例中

问题描述 投票:0回答:1

我正在尝试在我的swift项目中创建一个导航控制器。目前,我只有一个View Controller,并且到处都在尝试创建导航控制器。我用这些来尝试使其工作:Swift – Instantiating a navigation controller without storyboards in App Delegate

Creating a navigationController programmatically (Swift)

https://www.youtube.com/watch?v=7ZQMv1okhmI

但是这些都不适合我。

这是我的appDelegate:

    import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let mainVC = ViewController() as UIViewController
        let navigationController = UINavigationController(rootViewController: mainVC)
        navigationController.navigationBar.isTranslucent = false
        self.window?.rootViewController = navigationController
        self.window?.makeKeyAndVisible()
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

这是我的ViewController:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green
    }


}

这是我的模拟器中显示的内容:enter image description here

我只是好奇我做错了什么?我查找了几种以编程方式创建导航控制器的方法,这些方法最终都与我上面提供的示例相同。

如果还有其他需要帮助的地方,请询问,并且请勿将其标记为重复项,因为我知道之前曾有人问过这个问题,但是这些问题似乎都不适合我。

swift uinavigationcontroller
1个回答
0
投票

[如果您使用的是Xcode 11和iOS 13,则需要在Scene Delegate.swift中编写相同的代码在Learn App Making上了解有关App委托与场景委托的更多信息>

© www.soinside.com 2019 - 2024. All rights reserved.