簡體   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