I'm just starting out with Swift and SwiftUI, so I'm a total beginner. My SwiftUI project came with a LaunchScreen.storyboard which I thought would be nice to use, but I have no idea how. The app loads the initial ContentView.swift, even though I filled the LaunchScreen.storyboard with an image. Is there any place I need to specify the app should load the LaunchScreen first and then go to the ContentView after like, 2 seconds or something?
I suspect it's probably in SceneDelegate.swift, but I don't know.
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView.environmentObject(model))
self.window = window
window.makeKeyAndVisible()
}
Using XCode 11.2
Thanks in advance for any help!
Edit: It really is a cache issue. I deleted the ~/Library/Developer/Xcode/DerivedData/ModuleCache directory and removed the app from the simulator and test device. Then rebuilt and working! Thanks!
Although the Human Interface Guidelines recommend downplaying the launch experience, you can still implement a launch screen in a SwiftUI project.
Add a Launch Screen storyboard manually:
Is Initial View Controller to true:

Although this isn't technically the same as a Launch Screen because is really just swapping out the first screen of the app, it can achieve a similar effect:
struct ContainerView: View {
@State private var showSplashScreen = true
var body: some View {
ZStack {
if showSplashScreen {
LaunchScreenView()
.transition(.opacity)
} else {
ContentView()
.transition(.opacity)
}
}
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
withAnimation(.easeInOut(duration: 1)) {
showSplashScreen = false
}
}
}
}
}
struct LaunchScreenView: View {
var body: some View {
VStack {
Image("YourSplashImage")
.resizable()
.aspectRatio(contentMode: .fit)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.blue)
}
}
struct ContentView: View {
var body: some View {
Text("Main Content View")
.padding()
}
}
@main
struct YourAppName: App {
var body: some Scene {
WindowGroup {
ContainerView()
}
}
}
This setup allows you to implement a splash screen experience in SwiftUI, either with a timed transition or a manual dismissal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With