如何在视图中的单个 VStack 内添加多个按钮 (Xcode12, SwiftUI)
How to add multiple buttons inside a singular VStack in a View (Xcode12, SwiftUI)
我在单个 VStack 中有多个按钮时遇到问题。我收到错误:位置处的额外参数......下面是我的代码。此代码位于视图文件夹中,我正在使用 Xcode12 和 SwiftUI
确切的错误消息:调用中位置 #11、#12、#13 的额外参数
import SwiftUI
struct HomeView: View {
@Binding var page: Pages
var drink = Drink.AllDrinks
var body: some View {
VStack {
drink[0].image
.resizable()
.frame(width:80, height: 80)
Text(drink[0].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}
Spacer()
.frame(height: 35)
drink[1].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[1].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent
}
drink[2].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[2].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent
}
drink[7].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[7].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent
}
}
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView(page: .constant(Pages.Home))
}
}
struct OrderButtonContent: View {
var body: some View {
Text("Order")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 80, height: 30)
.background(Color.blue)
.cornerRadius(15.0)
}
}
就这么简单,让我们看看错误是什么:Exact error message: Extra arguments at positions #11, #12, #13 in call
上面写着额外参数!在 SwiftUI 中参数的最大数量是 10,我们应该为此使用 Group
,像这样:
struct HomeView: View {
@Binding var page: Pages
var drink = Drink.AllDrinks
var body: some View {
VStack {
Group { // <<: Here!
drink[0].image
.resizable()
.frame(width:80, height: 80)
Text(drink[0].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}
Spacer()
.frame(height: 35)
drink[1].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[1].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}
}
Group { // <<: Here!
drink[2].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[2].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}
drink[7].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[7].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}
}
}
}
}
我在单个 VStack 中有多个按钮时遇到问题。我收到错误:位置处的额外参数......下面是我的代码。此代码位于视图文件夹中,我正在使用 Xcode12 和 SwiftUI
确切的错误消息:调用中位置 #11、#12、#13 的额外参数
import SwiftUI
struct HomeView: View {
@Binding var page: Pages
var drink = Drink.AllDrinks
var body: some View {
VStack {
drink[0].image
.resizable()
.frame(width:80, height: 80)
Text(drink[0].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}
Spacer()
.frame(height: 35)
drink[1].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[1].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent
}
drink[2].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[2].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent
}
drink[7].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[7].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent
}
}
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView(page: .constant(Pages.Home))
}
}
struct OrderButtonContent: View {
var body: some View {
Text("Order")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 80, height: 30)
.background(Color.blue)
.cornerRadius(15.0)
}
}
就这么简单,让我们看看错误是什么:Exact error message: Extra arguments at positions #11, #12, #13 in call
上面写着额外参数!在 SwiftUI 中参数的最大数量是 10,我们应该为此使用 Group
,像这样:
struct HomeView: View {
@Binding var page: Pages
var drink = Drink.AllDrinks
var body: some View {
VStack {
Group { // <<: Here!
drink[0].image
.resizable()
.frame(width:80, height: 80)
Text(drink[0].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}
Spacer()
.frame(height: 35)
drink[1].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[1].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}
}
Group { // <<: Here!
drink[2].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[2].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}
drink[7].image
.resizable()
.frame(width: 80, height: 80)
Text(drink[7].name)
Button(action: {page = Pages.Home}) {
OrderButtonContent()
}
}
}
}
}