Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Launchscreen not showing

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!

like image 382
wildfang Avatar asked Jan 20 '26 00:01

wildfang


1 Answers

Although the Human Interface Guidelines recommend downplaying the launch experience, you can still implement a launch screen in a SwiftUI project.

Option 1. Using Storyboards

Add a Launch Screen storyboard manually:

  1. Add a new Storyboard file to your project.
  2. Configure your Storyboard with the content you want to see when your app first loads.
  3. IMPORTANT: In the property panel for your Storyboard, set Is Initial View Controller to true:

Screenshot of the Property panel showing "Is Initial View Controller" set to true.

  1. In your project settings, set Targets > General > App Icons and Launch Screen > Launch Screen File, select the .storyboard file you created.

Screenshot of Project Settings showing the Launch Screen set to the Storyboard file.

Option 2. Add a Launch Screen programmatically using SwiftUI.

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.

like image 106
cleverbit Avatar answered Jan 21 '26 21:01

cleverbit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!