如何在 SwiftUI 中从登录视图转换为 tabView [英] How do I transition from a login view to a tabView in SwiftUI
问题描述
我有一个 Google 登录屏幕,然后它应该会转换为 TabView.我尝试使用 ZStack,但它导致应用程序在加载每个选项卡之前出现故障.它将显示登录按钮一秒钟,然后会出现正确的选项卡.
I have a Google Sign-in screen, and then it should transition to a TabView. I tried using a ZStack, but it's causing the app to glitch before each tab is loaded. It will show the sign-in button for a second, and then the correct tab will appear.
有没有办法完全类似于 ViewControllers?或者有没有办法在我调用新视图 (MainView) 之前完全删除登录按钮?
Is there a way to totally segue similar to ViewControllers? Or is there a way to totally remove the sign-in button before I call the new view (MainView)?
MainView 只是一个带有 tabView 和 tabItems 的 SwiftUI 视图.
MainView is just a SwiftUI view with a tabView, and tabItems.
我有一个 SwiftUI SignInView:
I have a SwiftUI SignInView:
import SwiftUI
import GoogleSignIn
struct SignInView: View {
@State var loggedIn = false
let logo = Image('googleLogo')
var body: some View {
ZStack {
Button(action: {
self.signIn {
self.loggedIn = true
}
}, label: {
VStack {
logo
.padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
.background(Color.white)
.cornerRadius(8.0)
.shadow(radius: 4.0)
Text("Sign-In")
.foregroundColor(.primary)
.multilineTextAlignment(.center)
.padding(5)
}
}).zIndex(-1)
if loggedIn {
MainView()
}
}
}
func signIn(completion: @escaping () -> Void) {
GIDSignIn.sharedInstance()?.presentingViewController = UIApplication.shared.windows.last?.rootViewController
GIDSignIn.sharedInstance()?.signIn()
DispatchQueue(label: "SignIn Check", qos: DispatchQoS.background).async(execute: { () -> Void in
while true {
if GIDSignIn.sharedInstance()?.currentUser != nil {
completion()
break
}
}
})
}
}
struct SignInView_Previews: PreviewProvider {
static var previews: some View {
SignInView()
}
}
推荐答案
您可以保持 ZStack
的原样,并使登录按钮成为条件的一部分,以解决您看到的小故障,但更简洁的解决方案是使用 Group
而不是 ZStack
,其中包含登录条件:
You can keep your ZStack
the way it is and make the sign in button a part of the conditional too in order to fix the glitching you're seeing, but a cleaner solution would be to use a Group
instead of a ZStack
with the login conditional inside of it:
var body: some View {
Group {
if !loggedIn {
Button(action: {
self.signIn {
self.loggedIn = true
}
}, label: {
VStack {
logo
.padding(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16))
.background(Color.white)
.cornerRadius(8.0)
.shadow(radius: 4.0)
Text("Sign-In")
.foregroundColor(.primary)
.multilineTextAlignment(.center)
.padding(5)
}
})
} else {
MainView()
}
}
}
这篇关于如何在 SwiftUI 中从登录视图转换为 tabView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!