环境:Xcode 11.1在Catalina(19A573a)上运行并构建仅iOS应用。
我有以下应该很简单的代码。 -我有按钮A-H(8个按钮) -当我点击一个按钮时,我希望将它们带入各自的视图(“视图A”,“视图B”等),因为它们已嵌入到NavigationView中。
我遇到了几个问题 -在显示代码的情况下,轻按“视图A”的按钮不会起作用,而其他按钮则起作用。 -重新运行几次后,点击按钮A有时会起作用,但大多数情况下会失败 -如果我禁用除按钮A之外的所有其他按钮的显示,请点击按钮A。 -如果我禁用了任何一个按钮的显示(同样有8个按钮,A-H),那么点击第一个按钮即可。
我了解VStack的技术限制超过10个视图,我尝试了此处提出的建议: https://www.hackingwithswift.com/quick-start/swiftui/how-to-group-views-together 但这不起作用
我尝试了将按钮放在列表中的各种方法,但这不起作用。
任何要测试的人都可以创建一个新项目,并将整个代码内容复制到ContentView.swift文件中并运行。
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
HStack {
Spacer()
VStack {
Spacer()
Group {
NavigationLink(destination: ViewA()) {
BasicButton(buttonName: "View A", buttonColor: .orange)
}
NavigationLink(destination: ViewB()) {
BasicButton(buttonName: "View B", buttonColor: .red)
}
NavigationLink(destination: ViewC()) {
BasicButton(buttonName: "View C", buttonColor: .green)
}
NavigationLink(destination: ViewD()) {
BasicButton(buttonName: "View D", buttonColor: .blue)
}
}
Group {
NavigationLink(destination: ViewE()) {
BasicButton(buttonName: "View E", buttonColor: .pink)
}
NavigationLink(destination: ViewF()) {
BasicButton(buttonName: "View F", buttonColor: .gray)
}
NavigationLink(destination: ViewG()) {
BasicButton(buttonName: "View G", buttonColor: .purple)
}
NavigationLink(destination: ViewH()) {
BasicButton(buttonName: "View H", buttonColor: .yellow)
}
}
Spacer()
}
Spacer()
}
.background(Color.black).edgesIgnoringSafeArea(.all)
}
}
}
struct BasicButton: View {
var buttonName: String
var buttonColor: Color
var body: some View {
Text(buttonName)
.font(.title)
.multilineTextAlignment(.center)
.foregroundColor(.white)
.frame(width: 300, height: 60)
.background(buttonColor)
.cornerRadius(5)
.padding()
}
}
struct ViewA: View {
var body: some View {
Text("View A").font(.largeTitle)
}
}
struct ViewB: View {
var body: some View {
Text("View B").font(.largeTitle)
}
}
struct ViewC: View {
var body: some View {
Text("View C").font(.largeTitle)
}
}
struct ViewD: View {
var body: some View {
Text("View D").font(.largeTitle)
}
}
struct ViewE: View {
var body: some View {
Text("View E").font(.largeTitle)
}
}
struct ViewF: View {
var body: some View {
Text("View F").font(.largeTitle)
}
}
struct ViewG: View {
var body: some View {
Text("View G").font(.largeTitle)
}
}
struct ViewH: View {
var body: some View {
Text("View H").font(.largeTitle)
}
}