简体   繁体   中英

iOS Swift splash screen from Storyboard

In my iPhone app need to display a splash screen (displays company logo and some info) for 2-3 seconds before load the first page.

Also the app needs to decide which page should load as the first page in here (according the 'initial setup completion' level by the user).

I am using 'Swift' as the programing language and 'Universal StoryBoard' to design interfaces...

I have seleted the Main.storyboard as the Launch Screen File. In the ViewController class have implemented following logic

override func viewDidAppear(animated: Bool) {
    NSLog("Before sleep...")
    sleep(2)
    NSLog("After sleep...")

    self.controllNavigation()
}

func controllNavigation() -> Void {

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

    var nextViewController  

    if (Condition1)
    {
        nextViewController = storyBoard.instantiateViewControllerWithIdentifier("MainMenu") as! MainMenuViewController
    }
    else
    {
        nextViewController = storyBoard.instantiateViewControllerWithIdentifier("UserSetup") as! UserSetupViewController
    }

    self.presentViewController(nextViewController, animated: true, completion: nil)

}

All works ok but While waiting with sleep(2) , refresh page sort of a thing happens. I am not sure if this is the best way to do. Like to hear ideas. Thanks

I came across similar answers which mentioned sleep() , however it doesn't seem appropriate to use as it blocks the main thread.

I came up with this solution if you are using the default LaunchScreen.storyboard in Xcode 7.2:

func showLaunchScreen() {
    let launchScreen = UIStoryboard(name: "LaunchScreen", bundle: nil)
    let launchView = launchScreen.instantiateInitialViewController()
    self.view.addSubview(launchView!.view)

    let timeDelay = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC)))
    dispatch_after(timeDelay, dispatch_get_main_queue()) {
        UIView.animateWithDuration(0.5, animations: { _ in
            launchView?.view.alpha = 0.0
            }) { _ in
                launchView!.view.removeFromSuperview()
        }
    }

This will show the launch screen for another 2 seconds, then fade it out over 0.5 seconds. You can call you other function to load the 'default' VC in the completion handler.

Use the delay in app delegate instead of viewController's viewDidAppear. Use as:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    NSLog("Before Sleep");
    sleep(2);
    NSLog("After sleep");
    return true
}

Doing this will allow your splash screen to stay till 2 seconds and then you can use following in your view controller:

override func viewDidAppear(animated: Bool) {
   self.controllNavigation()
}

Hope this helps :)

I have solved this issue with this:

On my project I set the desired storyboard of splash (in my case Splash.storyboard) as Default Screen -> Targets/ (your target)/ Info/ Launch Screen Interface File Base Name.

Field on Project Settings

When you've done that, you can insert your logic on method "func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {} " placed on AppDelegate.swift .

If you want to make clean code, you can insert all your navigation related methods on another Class, and call them from AppDelegate.swift .

With this solution, the splash is visible only the required time, and when it finish, you can navigate to the screen you need.

Swift 4 Update 100% working

Just write one line of code Thread.sleep(forTimeInterval: 3.0) in the method of didfinishLauching.... in appdelegate class.

Example

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    Thread.sleep(forTimeInterval: 3.0)
    // Override point for customization after application launch.
    return true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM