Splash screen in SwiftUI with Example
To create a splash screen in SwiftUI, you can follow these steps:
Create a new SwiftUI view file called
SplashScreenView.swift
.In this file, define the
SplashScreenView
struct and add any necessary imports:
- Inside the
body
property ofSplashScreenView
, add any content you want to display on the splash screen. This could be an image, text, or any other SwiftUI view.
For example, you could display an image like this:
ContentView
file, add the following code to display the SplashScreenView
for a certain amount of time: In this code, the ContentView
struct uses a @State
variable called showSplashScreen
to determine whether to show the splash screen or the app's main content.
When showSplashScreen
is true
, the ZStack
displays the SplashScreenView
. The onAppear
modifier is used to schedule a closure that sets showSplashScreen
to false
after 2 seconds, with a fade animation.
When showSplashScreen
is false
, the ZStack
displays your app's main content.
Note: The withAnimation
closure is used to add a fade animation to the transition from the splash screen to the main content. If you don't want an animation, you can remove this closure.
Comments
Post a Comment