我是IOS和Swift UI的新手,我无法在启动画面后导航到登录屏幕。如何导航到登录屏幕?请帮帮我!!我真的很感谢你的所有回答。
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()
}
}
考虑到您可能不想在已经进入LoginScreen
后返回初始屏幕,我建议另一种方法,特别是更改SceneDelegate
中UIHostingController
rootView
。
如果要推送到下一个屏幕,可以使用以下代码重构:
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
}
}
}
这对我有用。
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")
}
}
}
我想你想要这样的东西:
导入 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
}
}
}
ContentView:
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: LoginView()) {
Text("Navigate")
}
}
.navigationBarTitle("LoginView")
.navigationBarHidden(true)
}
}
}
您的登录视图:
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)
}
}