Making Your LaunchScreen Useful

Add a little sugar to your LaunchScreen

2021 Update

If you don’t know what I mean, open Xcode then try to give a custom class to the view controller in the LaunchScreen.storyboard file. You’ll quickly find that you can’t do that.

The project won’t compile until you change it back. This makes adding animations or a delay before your app launches impossible — but you shouldn’t need to do that right?

In 2016, or 2017, I found a way around this to achieve a specification desired by a client of mine. And I’m going to share what I found then with you now.

*Please be aware this code has not been updated for Swift 5, only the article has.

As Easy As Pie

For this example, we’ll add a UIImageView centred in the canvas. Feel free to put your logo in here.

Note: Because this is essentially a horrible hack, the more elements you add and the more effects you want, the more code you'll end up write. The code won't be pretty. Feel free to tidy it up if you feel your skin crawling, or whatever.

Now, we’ll create a new Storyboard file. Call it what you want, but in this example I’ll call it "Root."

Open it up, and add a UINavigationController to it. Delete the associated UITableViewController and put a bog-standard UIViewController there in its place instead. Lastly, tell the navigation controller to hide its bar, that it’s the initial view controller by checking the box, and that the view controller you added is its rootViewController by control dragging from the navigation scene to the view controller.

With me so far?

Now let’s add a "Container View" with full screen constraints to the view controller, paying special attention to "Spacing to Safe Area" constraints. Add these via Control+Drag while holding down the Option (⌥) key, and make sure the spacing is set to “Space to Container.” Update the constants once you’re done.

Now add a storyboard reference to your LaunchScreen.storyboard. You’ll want to embed the reference with an Embed Segue.

Lastly, in your project configuration, set your initial interface to “Root”.

Once you’ve done all that, your Root.storyboard should look like something like this:

Breakpoint

Unwrap Sour Candy!

Note: You may need to update this a little for Swift 5.

This will allow us to let our new LaunchScreen pass the baguette onto the main app when the time comes.

Now create a new UIViewController in Xcode. Call it whatever you want, but try to keep the name somewhat relevant. Because naming things is hard, I’m going to use SplashScreenViewController.

Open it up and declare a couple of variables as follows:

var destination: UIViewController?
var logoImageView: UIImageView? // This is the image view from before

Now we want to hijack the prepareForSegue() method to actually capture the launch screen. Remember that “embed segue” we added before, and how I suggested to call it something memorable? We need it here, and for example purposes I called it EmbedLaunch .

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EmbedLaunch" { // put this string in your Constants, or something...
destination = segue.destinationViewController
logoImageView = destination?.subviews.first(where: { $0 as? UIImageView }) ?? nil
}
}

YAY! We now have a reference to the LaunchScreen as a UIViewController!

However, our UIImageView is still empty. Add a didSet block to the variable, and within it, set your image. Alternatively add a function to do it.

Enjoy Yourself

To finish this off, let’s animate the logo out of existence. Paste the following line beneath where the logo image is set.

performSelector(#selector(SplashScreenViewController.animateTheLogo), withObject: nil, afterDelay: 1.0)

Create a method to animateTheLogo() like so. Remember to keep all the storyboard identifiers correct, or you’ll crash. Have I mentioned I hate magic strings?

2021 Note: There are many extensions you can find online which make loading up storyboards a lot easier/cleaner.

This example should only be considered a proof of concept. There’re so many ways that you could make this better, and I look forward to seeing what you make with this.

Let me know how you do!

If you liked this post, please consider clapping for it. I notice each one and appreciate it a lot. Thank-you!

Aussie iOS Engineer based in Tokyo, Japan | 日英可

Aussie iOS Engineer based in Tokyo, Japan | 日英可