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:
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.