[英]Why is NavigationLink not navigation with SwiftUI
I am trying to use a NavigationLink to navigate to the LoginScreen.我正在尝试使用 NavigationLink 导航到 LoginScreen。 However clicking up on the button does not do anything.然而,点击按钮并没有做任何事情。 If I call the onTapGesture method then it does get triggered.如果我调用 onTapGesture 方法,它就会被触发。
struct SplashScreen: View {
@State
var shouldGoHome: Bool = false
@State
var goToDestination: Bool = false
@ObservedObject private var viewModel = SplashViewModel()
var body: some View {
VStack {
VColorBackground()
Text("VOWER")
.foregroundColor(Color(ColorTheme.brandBlue.value))
.font(.system(size: 36))
.tracking(20)
Text("YOUR TIME DESRVES A\n LOUDER APPLAUSE")
.tracking(2)
.multilineTextAlignment(.center)
.padding(EdgeInsets.init(top: 30, leading: 0, bottom: 0, trailing: 0))
.font(.system(size: 14))
.foregroundColor(Color.black)
if (viewModel.isUserLoggedIn) {
VowerNavigationButton(text: "Get Started", goToDestination: $goToDestination) {
AppView()
}
.padding(EdgeInsets.init(top: 70, leading: 0, bottom: 0, trailing: 0))
} else {
NavigationLink(destination: LoginScreen()) {
VowerButtonStyle(text: "Get Started")
}
.padding(EdgeInsets.init(top: 70, leading: 0, bottom: 0, trailing: 0))
}
Spacer()
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
}
}
You have to wrap the view inside a NavigationView { } otherwise, NavigationLink will not work.您必须将视图包装在 NavigationView { } 中,否则 NavigationLink 将无法工作。
struct SplashScreen: View {
@State
var shouldGoHome: Bool = false
@State
var goToDestination: Bool = false
@ObservedObject private var viewModel = SplashViewModel()
var body: some View {
NavigationView{
VStack {
VColorBackground()
Text("VOWER")
.foregroundColor(Color(ColorTheme.brandBlue.value))
.font(.system(size: 36))
.tracking(20)
Text("YOUR TIME DESRVES A\n LOUDER APPLAUSE")
.tracking(2)
.multilineTextAlignment(.center)
.padding(EdgeInsets.init(top: 30, leading: 0, bottom: 0, trailing: 0))
.font(.system(size: 14))
.foregroundColor(Color.black)
if (viewModel.isUserLoggedIn) {
VowerNavigationButton(text: "Get Started", goToDestination: $goToDestination) {
AppView()
}
.padding(EdgeInsets.init(top: 70, leading: 0, bottom: 0, trailing: 0))
} else {
NavigationLink(destination: LoginScreen()) {
VowerButtonStyle(text: "Get Started")
}
.padding(EdgeInsets.init(top: 70, leading: 0, bottom: 0, trailing: 0))
}
Spacer()
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
}
}
} }
For better understanding of Navigation, Please have a look at below code为了更好地理解导航,请看下面的代码
var body: some View {
NavigationView{
VStack(){
Text("VOWER")
NavigationLink(destination: detailView){
VStack(){
Text("Sunset").foregroundColor(Color.blue)
}
}.navigationBarTitle("Login") //define Title bar for better understanding and ease
}
}
}
Similarly in the destination view SwiftUI Class, you can define navigationBarTitle同样在目标视图SwiftUI类中,可以定义navigationBarTitle
var body: some View {
VStack(){
Text("Turn Notification On/Off")
}.navigationBarTitle("Settings")
}
- It works well on Device, Simulator has this bug of not working for second time. - 它在设备上运行良好,模拟器有第二次无法运行的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.