Search code examples
iosuiviewcontrollerswift3

How to make a view controller as splash screen in swift 3?


I am trying to make a view controller from storyboard as the splash screen in my application using swift 3. I am trying achieve the same effect as the splash screen does while loading the splash using "launch image". Since i have got an animation in the splash screen i wish to use a view controller as the splash screen in Swift 3. How to achieve this?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    self.window = UIWindow(frame: UIScreen.main.bounds)

    let storyboard = UIStoryboard(name: "Main", bundle: nil)

    let initialViewController = storyboard.instantiateViewController(withIdentifier: "splashScreen")

    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
    return true
}

Solution

  • Try making A View Controller as rootViewController having a splash screen and after 2-3 seconds of showing that View Controller. Push / Present your initial ViewController.

    You can set splash ViewController this way :-

    self.window?.rootViewController = splashVC
    

    In ViewDidLoad() of splashVC use timer or delay and after 2-3 seconds. Show your application's main View Controller.

    //MARK:- Life Cycle Methods
        override func viewDidLoad() {
            super.viewDidLoad()
            Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.splashTimeOut(sender:)), userInfo: nil, repeats: false)
            // Do any additional setup after loading the view.
        }
    
    func splashTimeOut(sender : Timer){
        AppDelegate.sharedInstance().window?.rootViewController = yourCustomViewController
        }
    

    Add this method in Appdelegate for your convenience :-

    class func sharedInstance() -> AppDelegate{
            return UIApplication.shared.delegate as! AppDelegate
        }