Launch Screen in SwiftUI
Creating an engaging launch screen is essential for providing users with a seamless experience as they open your app. In SwiftUI applications, you can configure the launch screen using two primary methods:
1. Configuring via Info.plist
2. Utilizing a LaunchScreen.storyboard File
This article will guide you through both approaches, complete with step-by-step instructions and examples.
1. Configuring the Launch Screen via Info.plist
Starting with Xcode 12 and iOS 14, Apple introduced a streamlined method to define launch screens directly within the Info.plist file. This approach is particularly beneficial for SwiftUI projects, as it eliminates the need for a storyboard file.
Steps:
Prepare Assets:
Background Color:
Open your project’s asset catalog (Assets.xcassets).
Create a new color set named LaunchScreenBackground.
Define colors for both Light and Dark appearances to ensure compatibility with system themes.
Launch Image:
Add an image to your asset catalog and name it LaunchImage.
Ensure the image is available in the necessary resolutions (1x, 2x, 3x) for different device screens. (Use .svg or .pdf for image)
Modify Info.plist:
Locate and open the Info.plist file in your project.
Add a new key named UILaunchScreen of type Dictionary.
Within this dictionary, add the following keys:
UIColorName (String): Set its value to LaunchScreenBackground.
UIImageName (String): Set its value to LaunchImage.
UIImageRespectsSafeAreaInsets (Boolean): Set its value to YES to ensure the image respects safe area insets.
Example Configuration:
<key>UILaunchScreen</key>
<dict>
<key>UIColorName</key>
<string>LaunchScreenBackground</string>
<key>UIImageName</key>
<string>LaunchImage</string>
<key>UIImageRespectsSafeAreaInsets</key>
<true/>
</dict>
Outcome:
With this setup, when users launch your app, they’ll see a screen with the specified background color and a centered image that adheres to safe area constraints.
Advantages:
Simplicity: Ideal for straightforward launch screens without complex layouts.
SwiftUI Alignment: Avoids the need to incorporate UIKit components into a SwiftUI project.project.
2. Utilizing a LaunchScreen.storyboard File
For more complex launch screen designs, employing a storyboard provides greater flexibility. This method allows you to design the launch screen visually using Interface Builder.
Steps:
Create a Launch Screen Storyboard:
In Xcode, navigate to File > New > File....
Under the User Interface section, select Storyboard and click Next.
Name the file LaunchScreen.storyboard and ensure it’s added to your project’s target.
Design the Launch Screen:
Open LaunchScreen.storyboard.
Use Interface Builder to design your launch screen, adding elements such as images, labels, or custom views.
Ensure that your design adheres to various device sizes and orientations by setting appropriate constraints.
Configure Project Settings:
Select your project in the Project Navigator.
Go to the General tab.
In the App Icons and Launch Images section, set the Launch Screen File to LaunchScreen.
Outcome:
This approach allows for a highly customizable launch screen, accommodating complex layouts and designs.
Advantages:
Flexibility: Supports intricate designs and layouts.
Visual Design: Utilizes Interface Builder for a visual design experience.
Considerations:
Complexity: May be more complex to implement compared to the Info.plist method.
SwiftUI Projects: Incorporates a UIKit component (Storyboard) into a SwiftUI project, which might not align with a pure SwiftUI approach.
Conclusion
Choosing between configuring the launch screen via Info.plist or using a LaunchScreen.storyboard depends on your app’s design requirements:
For Simple Designs: The Info.plist method offers a straightforward solution with minimal setup.
For Complex Designs: Utilizing a LaunchScreen.storyboard provides the flexibility needed for more intricate layouts.
By selecting the approach that best fits your project’s needs, you can create an effective and visually appealing launch screen that enhances the user experience from the moment they open your app.
Subscribe to my newsletter
Read articles from Nurul Islam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Nurul Islam
Nurul Islam
I'm an experienced mobile application developer with over 10 years of expertise in iOS development using Swift, Objective-C, and Flutter. Throughout my career, I've had the opportunity to work for multiple renowned companies where I developed and architected robust mobile applications used by millions of users worldwide. In addition to my extensive iOS experience, I also have experience working with ASP.Net Core, Entity Framework, and LINQ for backend development as well as Machine Learning and Deep Learning using Python and TensorFlow which has allowed me to collaborate closely with different teams to develop different types of products, apps or services.