Phu
Phu

Reputation: 123

Swift UI navigation from splash screen to another screen automatically with delay time

i am new in IOS and Swift UI, i cant navigate to login screen after splash screen. HOW CAN I NAVIGATE TO LOGIN SCREEN? please help me out!! i really appreciate all your answers.

import SwiftUI
import Dispatch

struct SplashScreen: View {
    let login = LoginScreen()
    let color = Color.init("black_1")
//    let color = Color.white
    var body: some View {
        let stack = VStack(alignment: .center){
            Image("logo")
            }.background(color).onAppear(perform: {
            gotoLoginScreen(time: 2.5)
        })

        return stack
    }
}

func gotoLoginScreen(time: Double){
    DispatchQueue.main.asyncAfter(deadline: .now() + Double(time)) {
        print("gotoLoginScreen")
    }
    return
}

struct SplashScreen_Previews: PreviewProvider {
    static var previews: some View {
        SplashScreen()
    }
}

Upvotes: 3

Views: 6224

Answers (4)

Antwon Key
Antwon Key

Reputation: 73

This worked for me.

import SwiftUI

struct ContentView: View {
    
    @State var isActive:Bool = false
    
    var body: some View {
        
        VStack {
            
            if self.isActive {
            
            LoginScreen()
            
            } else {
                
                Image("logo")
                
            }
            
        }.onAppear {
          
            DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
           
                withAnimation {
                    self.isActive = true
                }
            }
        
    }
    
    }
    
}


struct LoginScreen: View {
    var body: some View {
        
        VStack {
            
            Text("Welcome to")
            Text("the LoginView")
            
        }
        
    }
}

Upvotes: 2

gotnull
gotnull

Reputation: 27224

I think you want something like this:

import SwiftUI

struct LoginScreen: View {
    var body: some View {
        VStack {
            Text("Welcome to")
            Text("the LoginView")
        }.navigationBarTitle("Login")
    }
}

struct SplashScreen: View {
    @State private var isActive = false

    var body: some View {
        NavigationView {
            VStack {
                Image("logo")
                NavigationLink(destination: LoginScreen(),
                               isActive: $isActive,
                               label: { EmptyView() })
            }
            .onAppear(perform: {
                self.gotoLoginScreen(time: 2.5)
            })
        }
    }

    func gotoLoginScreen(time: Double) {
        DispatchQueue.main.asyncAfter(deadline: .now() + Double(time)) {
            self.isActive = true
        }
    }
}

Upvotes: 1

Milen Valchev
Milen Valchev

Reputation: 187

Considering that you probably won't want to go back to the splash screen after you are already on the LoginScreen I would suggest another approach and in particular to change the rootView of the UIHostingController in SceneDelegate.

If you want to push to the next screen you can use the following refactor of your code:

struct SplashScreen: View {
    @State private var isActive = false
    let login = LoginScreen()
    let color = Color.init("black_1")

    var body: some View {
        NavigationView {
            VStack(alignment: .center) {
                Image("logo")
                NavigationLink(destination: login,
                               isActive: $isActive,
                               label: { EmptyView() })
            }
            .background(color)
            .onAppear(perform: {
                self.gotoLoginScreen(time: 2.5)
            })
        }
    }

    func gotoLoginScreen(time: Double) {
        DispatchQueue.main.asyncAfter(deadline: .now() + Double(time)) {
            self.isActive = true
        }
    }
}

Upvotes: 6

M A Russel
M A Russel

Reputation: 1557

ContentView:

struct ContentView: View {
var body: some View {
    NavigationView {
        VStack {
            NavigationLink(destination: LoginView()) {
                Text("Navigate")
            }
        }
        .navigationBarTitle("LoginView")
        .navigationBarHidden(true)
    }
  }
}

Your LoginView:

struct LoginView: View {
 @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

  var body: some View {

      Button(action: {
         self.presentationMode.wrappedValue.dismiss()
      }) {
          Image(systemName: "gobackward").padding()
      }
      .navigationBarHidden(true)

  }
}

Upvotes: -2

Related Questions