1

I am building an iOS app in swift and upon launching the app I would like to first show viewController2, which is presented on top of viewController1, which is embedded in a UINavigationController. The key part being that ViewController2 is not part of the navigation stack and is presented instead of being pushed.

This is my current attempt which doesn't work and only shows ViewController1 upon launch.

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController1 = mainStoryboard.instantiateViewController(withIdentifier: "VC1") as! ViewController1
        let viewController2 = mainStoryboard.instantiateViewController(withIdentifier: "VC2") as! ViewController2
        let navController = UINavigationController(rootViewController: viewController1)
        viewController1.present(viewController2, animated: false)

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

        return true
    }

I have this structure in place because I enable a user to be able to swipe from left to right and right to left to get to viewController2 and viewController3 in a similar fashion to Snapchat. Presenting these controllers seemed the best idea as they don't have navigation bars and once finished you would want to return to viewControlller1 - perhaps I need to change the structure of my app but would ideally not like to as I aim to submit this mvp within the next week.

Help much appreciated from you iOS and swift wizards.

// Think I need to change the structure of the app to include viewController2 on the navigation stack :( as using accepted answer causes viewController1 to be seen briefly before viewController2 is presented which is logical - in the long run this will be better despite the short term pain

Edward
  • 2,864
  • 2
  • 29
  • 39
  • For such questions I always recommend to see [this question](https://stackoverflow.com/questions/19962276/best-practices-for-storyboard-login-screen-handling-clearing-of-data-upon-logou) and its many answers – mfaani Dec 30 '16 at 16:49

1 Answers1

1

rootViewController should be set before everything. Do it in this order and it should work fine.

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

navController.present(viewController2, animated: false)
Matt
  • 1,711
  • 18
  • 20
  • Thanks @Matt - Was tearing my hair out wandering why it wouldn't work but all makes sense now – Edward Dec 30 '16 at 16:56
  • Only issue now is that viewController1 can be seen just before viewController2 is presented, which in this case isn't ideal – Edward Dec 30 '16 at 17:04
  • If you do something in your viewDidLoad method in ViewController2, try commenting them and see if there's any improvement. – Matt Dec 30 '16 at 17:27