我只是想在用户单击按钮时打开一个新视图。使用 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)
}
谢谢。
以赛亚·汤普森
最好的方法是使用
NavigationLink
并将内容包装在 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)
}
}
}
}
您可以在官方文档中找到更多信息:
另外 [Hacking With Swift] (https://www.hackingwithswift.com/quick-start/swiftui/displaying-a-detail-screen-with-navigationlink) 是 SwiftUI 的重要资源
Button {
showLoginView = true
} label: {
Text("Login")
}
NavigationLink("", destination: LoginView(), isActive: $showLoginView)