-1

I am trying to create a navigation controller in my swift project. At this time I only have one View Controller, and I looked all over the place to try and create the navigation controller. I used these to try and get it to work: Swift – Instantiating a navigation controller without storyboards in App Delegate

Creating a navigationController programmatically (Swift)

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

but none of these work for me.

this is my 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.
    }


}

and this is my ViewController:

import UIKit

class ViewController: UIViewController {

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


}

this is what appears in my simulator: enter image description here

I am just curious what i did wrong? I looked up a few ways to create the navigation controller programmatically and they all ended up the same way as the example I provided above.

if there is anything else i can help with please ask, and please don't mark as duplicate because I know that this question has been asked before but none of those seems to work for me.

Yo19
  • 223
  • 2
  • 16

1 Answers1

1

If you are using Xcode 11 and iOS 13, you need to write same code in SceneDelegate.swift. The main reason for Apple to add UISceneDelegate to iOS 13 was to create a good entry point for multi-windowed applications.

If you do not intend to support multiple windows, you can remove SceneDelegate.swift file remove UIApplicationSceneManifest key from Info.plist and remove func application(_: configurationForConnecting:, options:) and func application(_:, didDiscardSceneSessions:) methods from AppDelegate.swift

Learn more about Scene Delegate on donnywalls - Understanding the iOS 13 Scene Delegate