swift 如何切换到一个新的视图上简单的按钮点击?

pu3pd22g  于 2022-12-02  发布在  Swift
关注(0)|答案(1)|浏览(177)

我只是尝试在用户点击按钮时打开一个新的视图。使用Swift API做这件事最简单、最容易的方法是什么?

//Login Button
            Button(
                action:
                {
                // Open Signup Screen
                    Signup();
                },
                label:
                {
                // How the button looks li
                Text("Create New Account")
                .foregroundColor(.white)
                .font(.title)
                }
                )
                .frame(maxWidth: .infinity, alignment: .center)
                .background(Color(red: 0.22, green: 0.655, blue: 0.02))
                .cornerRadius(8)
                .padding(.horizontal, metrics.size.width*0.10)
            
        }
  • 谢谢-谢谢
    以赛亚·汤普森
ruarlubt

ruarlubt1#

最好的方法是使用NavigationLink并将内容 Package 在NavigationView中。
NavigationView用于表示视图层次结构。
例如:

// Wrapper for LogIn
struct ContentView: View {
    var body: some View {
      NavigationView { LogIn() }
    }
}

// LogIn
struct LogIn: View {
  var body: some View {
    VStack {
      // Fields for log in

      NavigationLink(destination: SignUp()) {
        Text("Create New Account")
          .foregroundColor(.white)
          .font(.title)
          .frame(maxWidth: .infinity, alignment: .center)
          .background(Color(red: 0.22, green: 0.655, blue: 0.02))
          .cornerRadius(8)
          .padding(.horizontal, metrics.size.width*0.10)
      }
    }
  }
}

您可以在官方文档中找到更多信息:

此外,[使用Swift进行黑客攻击](https://www.hackingwithswift.com/quick-start/swiftui/displaying-a-detail-screen-with-navigationlink)是SwiftUI的一个很好的资源

相关问题