Creating a navigationController programmatically (Swift)

后端 未结 6 372
盖世英雄少女心
盖世英雄少女心 2020-11-29 19:21

I\'ve been trying to redo the work on my app programmatically. (Without the use of storyboards)

I\'m almost done, except making the navigation controller manually.

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 19:23

    Swift 1, 2:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
       self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
       var nav1 = UINavigationController()
       var mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
       nav1.viewControllers = [mainView]
       self.window!.rootViewController = nav1
       self.window?.makeKeyAndVisible()
    }
    

    Swift 4+: and Swift 5+

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
       self.window = UIWindow(frame: UIScreen.main.bounds)
       let nav1 = UINavigationController()
       let mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
       nav1.viewControllers = [mainView]
       self.window!.rootViewController = nav1
       self.window?.makeKeyAndVisible()
    }
    

提交回复
热议问题