Reputation: 123
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
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
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
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
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