【问题标题】:How To Switch To A New View On Simple Button Click?如何在简单按钮单击时切换到新视图?
【发布时间】:2021-12-29 00:35:30
【问题描述】:

我只是想在用户单击按钮时打开一个新视图。使用 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)
            
        }

谢谢。

以赛亚·汤普森

【问题讨论】:

标签: ios swift swiftui


【解决方案1】:

最好的方法是使用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 的一个很好的资源

【讨论】:

  • 非常感谢!代码也很干净
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-21
  • 1970-01-01
相关资源
最近更新 更多