繁体   English   中英

无法使用 NavigationLink + SwiftUI 导航到另一个屏幕

[英]Not able to navigate to another screen using NavigationLink + SwiftUI

我想从一个屏幕导航到另一个屏幕。 所以我使用了 NavigationLink。 每当我单击按钮时,什么都没有发生,甚至打印语句都没有在控制台中打印。

这是代码:

struct _LoginView: View {

  var body: some View {

    Color.green
        .edgesIgnoringSafeArea(.all)
        .overlay(
            VStack(alignment: .center, spacing: 15) {

                Image("Bg")
                    .resizable()
                    .scaledToFill()
                    .frame(width: 130, height: 130, alignment: .center)
                    .clipShape(Circle())

                Spacer()

                NavigationLink(destination: _LoginViewMain()) {

                    Button(action: {
                        print("button clicked")
                    }) {
                        Text("SIGN IN")
                            .frame(minWidth: 0, maxWidth: .infinity)
                            .font(.system(size: 18))
                            .padding()
                            .foregroundColor(.white)
                            .overlay( RoundedRectangle(cornerRadius: 25)
                                    .stroke(Color.white, lineWidth: 2)
                        )
                    }
                }

                NavigationLink(destination: _LoginViewMain()) {

                    Button(action: {
                        print("btn cliecked")
                    }) {
                        Text("SIGN UP")
                            .frame(minWidth: 0, maxWidth: .infinity)
                            .font(.system(size: 18))
                            .padding()
                            .foregroundColor(.green)

                    }
                    .background(Color.white)
                    .cornerRadius(25)
                }
            }
            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
            .padding([.leading, .trailing], 30)
            .foregroundColor(Color.green)
            .padding([.top, .bottom], 100)
    )
  }

}

有人可以帮我知道导航链接没有被触发的原因吗?

您必须将 NavigationLink 嵌入到 NavigationView 中。 请尝试下面的代码段。

struct _LoginView: View {
    var body: some View {
        NavigationView {
            Color.green
                .edgesIgnoringSafeArea(.all)
                .overlay(
                    VStack(alignment: .center, spacing: 15) {

                        Image("Bg")
                            .resizable()
                            .scaledToFill()
                            .frame(width: 130, height: 130, alignment: .center)
                            .clipShape(Circle())

                        Spacer()


                        Button(action: {
                            print("button clicked")
                        }) {
                            NavigationLink(destination: _LoginViewMain()) {
                                Text("SIGN IN")
                                    .frame(minWidth: 0, maxWidth: .infinity)
                                    .font(.system(size: 18))
                                    .padding()
                                    .foregroundColor(.white)
                                    .overlay( RoundedRectangle(cornerRadius: 25)
                                        .stroke(Color.white, lineWidth: 2)
                                )
                            }
                        }


                        Button(action: {
                            print("btn cliecked")
                        }) {
                            NavigationLink(destination: _LoginViewMain()) {
                                Text("SIGN UP")
                                    .frame(minWidth: 0, maxWidth: .infinity)
                                    .font(.system(size: 18))
                                    .padding()
                                    .foregroundColor(.green)

                            }
                            .background(Color.white)
                            .cornerRadius(25)
                        }
                    }

                    .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
                    .padding([.leading, .trailing], 30)
                    .foregroundColor(Color.green)
                    .padding([.top, .bottom], 100)
            )
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM